http://bailian.openjudge.cn/practice/2797/
#include<iostream>
using namespace std;
struct trie{
int count;
trie *next[26];
trie(){
count=0;
int i=0;
for(i=0;i<26;i++){
next[i]=NULL;
}
}
}root;
char word[1001][21];
void insert(char w[]){
trie *r = &root;
int i;
for(i=0;w[i];i++){
int t = w[i]-'a';
if(r->next[t]==NULL){
r->next[t] = new trie;
r->next[t]->count = 1;
}else if(r->next[t])
r->next[t]->count++;
r = r->next[t];
}
}
void search(char w[]){
trie *r = &root;
if(r == NULL)
return;
int i;
for(i=0;w[i];i++){
int t = w[i]-'a';
if(r->next[t]==NULL)
return;
cout<<w[i];
if(r->next[t]->count==1)
break;
r = r->next[t];
}
}
int main(){
int i,n=1;
while(cin>>word[n]){
if(word[n][0]=='0')
break;
insert(word[n++]);
}
for(i=1;i<n;i++){
cout<<word[i]<<" ";
search(word[i]);
cout<<endl;
}
return 0;
}