您的位置:首页 > 理论基础 > 计算机网络

poj2001 http://poj.org/problem?id=2001 简单的字典树题目

2012-10-26 19:25 465 查看
暑假集训的时候 做了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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: