求助字典树
查看原帖
求助字典树
592662
zhaoxibo楼主2023/1/13 17:53
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int t,n;
int ch[10005][125],tot=1;
char a[125];
bool last[10005];
bool pan(char s[]){
	int len=strlen(s);int u=1;bool flag=false;
	for(int i=0;i<len;i++){
		int x=s[i];
		if(!ch[u][x]) ch[u][x]=++tot;
		else if(i==len-1) flag=true;
		u=ch[u][x];
		if(last[u]) flag=true;
	}
	last[u]=true;
	return flag;
}
int main(){
	scanf("%d",&t);
	while(t--){
		memset(ch,0,sizeof(ch));
		memset(last,false,sizeof(last));
		scanf("%d",&n);
		bool ans=false;
		for(int i=1;i<=n;i++){
			cin>>a;
			if(pan(a)) ans=true;
		}
		if(ans) printf("NO\n");
		else printf("YES\n");
	}
}
2023/1/13 17:53
加载中...