题目地址
http://acm.hdu.edu.cn/showproblem.php?pid=2397
我的代码:
#include<iostream>
#include<string.h>
using namespace std;
char word[105];
char name[105];
struct tree
{
tree *next[26];
int c;
};
tree *newtree()
{
int i;
tree *s=new tree;
for(i=0;i<26;i++)
s->next[i]=NULL;
s->c=0;
return s;
}
void creat(tree *s,char *str)
{
int i;
tree *newnode;
if(*str==0)return;
for(i=0;str[i];i++)
if(islower(str[i]))
str[i]-=32;
for(i=0;str[i];i++)
{
if(!isupper(str[i]))
continue;
if(s->next[str[i]-'A']!=NULL)
{
s=s->next[str[i]-'A'];
}
else
{
newnode=newtree();
s->next[str[i]-'A']=newnode;
s=newnode;
}
}
s->c=1;
}
void search(tree *root,int t)
{
int i;
for(i=0;i<26;i++)
{
if(root->next[i])
{
word[t]=i+'A';
if(root->next[i]->c==1)
{
word[t+1]='\0';
printf("%s\n",word);
root->next[i]->c=0;
}
search(root->next[i],t+1);
}
}
}
int main()
{
int i,k,t=0;
int d=1;
tree *root=newtree();
while(scanf("%s",name)!=EOF)
{
if(strcmp(name,"EOD")&&strcmp(name,"EOT"))
{
t=1;
int a[30];
a[0]=0;
for(i=0,k=-1;name[i];i++)
{
if(name[i]=='-')
{
k=i+1;
a[t++]=k;
name[i]='\0';
}
}
for(i=0;i<t;i++)
creat(root,name+a[i]);
}
else
{
printf("WORDS IN DOCUMENT #%d\n",d++);
search(root,0);
if(strcmp(name,"EOT")==0)break;
}
}
return 0;
}