哎,开始理解错题意了,还以为找的是已经存在的字符串, 搞得连个测试数据都过不了,结果看了下别人的博客,才发现又会错了意,哎,做题须谨慎啊。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N = 3010;
struct Node{
int flag;
int count;
Node *next[26];
}root;
int tot;
Node node[N];
char word[1010][25];
void insert(char *str,int n)
{
Node *p = &root;
while(*str)
{
if(p->next[*str - 'a'] == NULL)
{
memset(node[tot].next, NULL, sizeof(node[tot].next));
node[tot].flag = 0;
node[tot].count = 0;
p->next[*str - 'a'] = &node[tot++];
}
p = p->next[*str - 'a'];
p->count++;
str++;
}
p->flag = n;
}
void search(char *str)
{
Node *p = &root;
while(*str&&p)
{
if(p->count==1)
break;
printf("%c",*str);
p = p->next[*str - 'a'];
str++;
}
putchar(10);
}
int main(void)
{
int n = 0;
tot = 0;
memset(root.next, NULL, sizeof(root.next));
while(scanf("%s",word[++n])!=EOF)
{
//cout<<"ok"<<endl;
insert(word[n], n);
}
for(int i=1; i<=n; ++i)
{
printf("%s ",word[i]);
search(word[i]);
}
return 0;
}