题目大意:
- 给 n 串电话号码 , 如果有任意号码是其他号码的前缀,就输出 NO ,否则输出 YES
解题过程:
- 思路是利用trie树(字典树)来解决.
- 但在写代码的过程中,开始是每次遇到错误的情况就输出并且break,这到这了我不会将所有的电话号码都读入,而将一部分电话号码放到下一组数据的bug.
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
#define rep(i,l,p) for(int i =l;i<=p;i++)
#define fread() freopen("in.txt","r",stdin)
int T,n;
int tr[100005][11];
bool endf[100005];
int tot,nx;
char s[15];
int main(int argc, char const *argv[])
{
scanf("%d",&T);
while(T--){
memset(endf,false,sizeof(endf));
memset(tr,0,sizeof(tr));
tot = 0;
scanf("%d",&n);
bool flag = true;
rep(i,1,n){
scanf("%s",s);
nx = 0;
int len = strlen(s);
int num;
rep(j,0,len-1){
num = s[j] - '0';
if( j == len-1 && tr[nx][num] != 0){
flag = false;
}
if(tr[nx][num] == 0) tr[nx][num] = ++tot;
nx = tr[nx][num];
if(endf[nx]){
flag = false;
}
}
endf[nx] = true;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}