Phone List HDU - 1671 字典树

时间:2020-12-08 03:38:11

题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀

思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含

刚开始开的1e4死活不过  1e5直接过了。。

 #include<bits/stdc++.h>
#include<string>
using namespace std;
const int maxn=1e5+;
struct Trie{
int size;
int ch[maxn][];
int isEnd[maxn];
void init(){
memset(ch,,sizeof(ch));
memset(isEnd,,sizeof(isEnd));
size=;
}
bool insert(char*s){
int rc=,i=;
int flag=;
for(;s[i]!='\0';i++){
int id=s[i]-'';
if(ch[rc][id]==){
ch[rc][id]=size++;
flag=;
}
if(isEnd[rc]==)return ;
rc=ch[rc][id];
//if(isEnd[rc]==1)return 1; }
isEnd[rc]=;
return flag;
}
}trie;
char s[];
int main(){
int t;
scanf("%d",&t);
while(t--){
int ok=;
int n;
scanf("%d",&n);
trie.init();
for(int i=;i<n;i++){
scanf("%s",s);
if(ok)continue;
if(trie.insert(s))ok=;
}
if(ok)printf("NO\n");
else printf("YES\n");
}
return ;
}