#include <iostream>
const int MAXN = 1005;
const int MAXM = 21;
struct node
{
int cnt;
node* next[26];
node()
{
cnt = 0;
memset(next, 0, sizeof(next));
}
}* root;
void insert(char s[])
{
node* cur = root;
for(int i = 0; i < (int)strlen(s); i++)
{
int id = s[i] - 'a';
if(cur->next[id] == NULL)
{
cur->next[id] = new node();
}
cur = cur->next[id];
cur->cnt++;
}
}
void printStr(char s[], int len)
{
printf("%s ", s);
for(int i = 0; i < len; i++)
{
printf("%c", s[i]);
}
printf("\n");
}
void find(char s[])
{
node* cur = root;
for(int i = 0; i < (int)strlen(s); i++)
{
int id = s[i] - 'a';
if(cur->next[id]->cnt == 1)
{
printStr(s, i + 1);
return;
}
cur = cur->next[id];
}
printf("%s %s\n", s, s);
}
char s[MAXN][MAXM];
int main()
{
root = new node();
int strNum = 0;
while(scanf("%s", s[strNum]) != EOF)
{
insert(s[strNum]);
strNum++;
}
for(int k = 0; k < strNum; k++)
{
find(s[k]);
}
return 0;
}
POJ 2001 Trie树 (EOF 用ctrl+z表示 VS中) 一次AC!
最新推荐文章于 2020-11-17 00:15:27 发布