#include<stdio.h>
#include<string.h>
char str[50001][50];
char map[1005][1005];
char s[1005];
int m,n,t;
int sx,sy;
int div[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
struct ANS
{
int x,y;
char towards;
}ans[1005];
struct tree
{
tree *next[30];
int id;
int time;
tree()
{
id=0;
for(int i=0;i<30;i++)
{
next[i]=NULL;
}
}
}*root;
void set(char *s)
{
tree *item=root;
for(int i=0;s[i]!='\0';i++)
{
int j=s[i]-'a';
if(item->next[j]==NULL)
{
item->next[j]=new tree;
item->next[j]->id=1;
}
item=item->next[j];
}
item->id=0;
}
int search(tree *item,char *s)
{
for(int i=0;s[i]!='\0';i++)
{
int j=s[i]-'a';
if(item==NULL||item->next[j]==NULL)
{
return 1;
}
item=item->next[j];
}
return item->id;
}
int main()
{
root=new tree;
int t;
int j,i=0;
while(scanf("%s",str[i])!=EOF)
{
set(str[i]);
i++;
}
t=i;
for(i=0;i<t;i++)
{
for(j=1;j<strlen(str[i]);j++)
{
char item1[50]={'\0'};
char item2[50]={'\0'};
strncpy(item1,str[i],j);
strncpy(item2,str[i]+j,strlen(str[i])-j);
if((search(root,item1)==0)&&(search(root,item2)==0))
{
printf("%s\n",str[i]);
break;
}
}
}
}
Hdu1247 - Hat’s Words - 字典树
最新推荐文章于 2020-07-30 10:17:37 发布