暑假集训的时候 做了10道字典树的题目 今天发现遗忘的狠厉害啊 所以 做了道简单的找感觉。
#include<stdio.h>
#include<string.h>
struct node
{
int count;
node *next[26];
} root,Root[26000];
int big=0;
char work[1010][21];
void buildTree(char *root1)
{
node *start=&root;
while(*root1)
{
if(start->next[*root1-'a']==NULL)
{
Root[big].count=0;
start->next[*root1-'a']=&Root[big++];
}
start=start->next[*root1-'a'];
start->count++;
root1++;
}
return ;
}
void search(char *root1)
{
node *start=&root;
while(*root1)
{
if(start->count==1)
break;
printf("%c",*root1);
start=start->next[*root1-'a'];
if(start==NULL)
break;
root1++;
}
}
int main()
{
int k=0;
while(scanf("%s",work[k])!=EOF)
{
buildTree(work[k]);
k++;
}
for(int i=0; i<k; i++)
{
printf("%s ",work[i]);
search(work[i]);
printf("\n");
}
return 0;
}