**2359 - 数字串前缀匹配 ---trie字典树
**来源:东方博宜oj oj.czos.cn
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char s[15];
int cnt[N];
int ch[N][26],idx=0;
int T,n;
bool insertt(char s[])
{
int p=0;
bool f1=false,f2=false;
for(int i=0;s[i]!='\0';i++)
{
int x=s[i]-'0';
if(!ch[p][x])
{
ch[p][x]=++idx;
f1=true;
}
p=ch[p][x];
if(cnt[p]) f2=true;
}
cnt[p]++;
return (f1==false)||f2;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(ch,0,sizeof(ch));
memset(cnt,0,sizeof(cnt));
idx=0;
bool f=false;
for(int i=1;i<=n;i++)
{
scanf("%s",s);
if(insertt(s)) f=true;
}
if(f) printf("NO\n");
else printf("YES\n");
}
return 0;
}
2359 - 数字串前缀匹配 ---trie字典树
最新推荐文章于 2024-07-17 18:07:42 发布