Shortest Prefixes
很简单的字典树
不知道为什么,写了struct就会TLE
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
using namespace std;
int vis[100001];
int ne[100001][31];
int Aimee=1;
int x;
int Ai;
char ss[1101][50];
void add(char s[]){
int Archie=0;
for(int i=0;i<s[i];++i){
int x=s[i]-'a';
if(!ne[Archie][x]){
Aimee++;
ne[Archie][x]=Aimee;
}
Archie=ne[Archie][x];
vis[Archie]++;
}
}
string find(char s[]){
int Archie=0;
string ans;
for(int i=0;i<s[i];++i){
int x=s[i]-'a';
if(vis[Archie]==1){
return ans;
}
ans+=s[i];
Archie=ne[Archie][x];
}
return ans;
}
int main(){
while(scanf("%s",ss[Ai])!=EOF){
add(ss[Ai]);
Ai++;
}
for(int i=0;i<Ai;++i){
cout<<ss[i]<<" "<<find(ss[i])<<endl;
}
return 0;
}