字典树模板
poj3630
#include <cstdio>
#include <algorithm>
#include<cmath>
#include<string.h>
#include<memory.h>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include <stdio.h>
#include <stack>
#include <set>
#define MOd 0x7FFFFFFF;
#define mod 65537;
using namespace std;
typedef long long LL;
const int N=1e9+7;
char ss[10005][15];
int t,n;
struct tnode{
int cnt;
tnode *next[15];
}root,node[100005];
int num=0;
tnode *inittree(){
tnode *p=&node[num++];
p->cnt=0;
for(int i=0;i<10;i++)
p->next[i]=NULL;
return p;
}
void maketree(tnode *&root,char *p){
if(root==NULL )
root=inittree();
tnode *t=root;
while(*p){
if(t->next[*p-'0']==NULL){
t->next[*p-'0']=inittree();
}
t=t->next[*p-'0'];
t->cnt++;
p++;
}
}
bool seah(tnode *root,char *p){
//cout<<"r"<<endl;
//cout<<p<<endl;
if(root==NULL)
return true;
//cout<<"r"<<endl;
tnode *t=root;
while(*p){
//cout<<"r"<<endl;
//cout<<*p<<"p"<<endl;
t=t->next[*p-'0'];
//cout<< t->cnt <<" "<<*p<<endl;
p++;
}
if(t->cnt>1)
return false;
else
return true;
}
int main()
{
// freopen("in.txt","r",stdin);
cin>>t;
tnode *root=NULL;
while(t--){
root=NULL;
scanf("%d",&n);
int i;
for(i=0;i<n;i++){
scanf("%s",ss[i]);
maketree(root,ss[i]);
}
for(i=0;i<n;i++){
if(!seah(root,ss[i])){
cout<<"NO"<<endl;
break;
}
}
if(i==n)
cout<<"YES"<<endl;
num=0;
}
return 0;
}