原题:http://poj.org/problem?id=3630
题解:将所有数字建成字典树,当插入时无新节点生成,经过别的有标记的节点时,就是前缀。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=1e5+10,Z=10;
int ch[N][Z],cas,n,tot;
bool mark[N],flag;
char s[Z<<1];
inline void clear(){
tot=1;flag=0;
memset(ch,0,sizeof ch);
memset(mark,0,sizeof mark);
}
bool ins(char *s){
int len=strlen(s+1);
bool f=0;int p=1;int last=tot;
for(int i=1;i<=len;i++){
int c=s[i]-'0';
if(!ch[p][c])ch[p][c]= ++tot;
p=ch[p][c];
if(mark[p]) f=1;
}
mark[p]=1;
if(last==tot) f=1;
return f;
}
int main(){
//freopen("poj3630.in","r",stdin);
scanf("%d",&cas);
while(cas--){
clear();
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s+1);
if(ins(s)) flag=1;
}
if(flag) puts("NO");
else puts("YES");
}
return 0;
}