1471 [一本通例题1]Phone List

1471:【例题1】Phone List


时间限制: 1000 ms         内存限制: 65536 KB
提交数: 2918     通过数: 1005

【题目描述】

原题来自:POJ 3630

给定 n 个长度不超过 10 的数字串,问其中是否存在两个数字串 S,T,使得 S 是 T 的前缀,多组数据。

【输入】

第一行一个整数 T,表示数据组数。

对于每组数据,第一行一个数 n,接下来 n 行输入 n 个数字串。

【输出】

对于每组数据,若存在两个数字串 S,T,使得 S 是 T 的前缀,则输出 NO ,否则输出 YES 。

请注意此处结果与输出的对应关系!

【输入样例】

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

【输出样例】

NO
YES

【提示】

数据范围:

对于 100% 的数据,1≤T≤40,1≤n≤104​​ 。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int const N=1e5+5;
int ch[N][11],tot=1;
bool bo[N];
char a[101];
bool word1(char *s){
	int l=strlen(s);
	bool f=false;
	int u=1;
		for(int i=0;i<l;i++){
			int v=int(s[i])-'0';
			if(!ch[u][v]){
				ch[u][v]=++tot;
			}
			else if(i==l-1){
				f=true;
			}
			u=ch[u][v];
			if(bo[u]==true)f=true;
		}
		bo[u]=true;
		return f;
}
int main()
{
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		memset(ch,0,sizeof(ch));
		memset(bo,false,sizeof(bo));
		bool o=false;
		tot=1;
		for(int k=1;k<=n;k++){
			scanf("%s",a);
			if(word1(a))o=true;
		}
		if(o==true){
			cout<<"NO"<<endl;
		}
		else cout<<"YES"<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值