zoj 3784 String of Infinity(难题,方法妙)

91 篇文章 1 订阅
12 篇文章 0 订阅

Given a set of banned words S, please find out whether it is possible to construct a string str1..∞ with infinite length that fulfills the following constrains:

  1. It consists of only the first M types of lowercase letters in the alphabet. For example M = 3, only 'a', 'b' and 'c' are allowed to appear in the string.
  2. There does not exist such (ij) that stri..j is a banned word in S (1 <= i <= j < ∞).
  3. There does not exist such (ij) that for any k >= istrk = str(j + k) (1 <= ij < ∞).

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N (1 <= N <= 100) and M (1 <= M <= 26). The following N lines, each line contains contains a non-empty string indicating a banned word in S. The length of each word will not exceed 1000 and the word only consists of lowercase letters.

Output

For each test case, output "Yes" if it is possible to construct such a string, otherwise "No".

Sample Input
2
2 2
aa
bb
1 2
aa
Sample Output
No

Yes

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5271

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		int wo[26]={0},word[26][26];
		memset(word,0,sizeof(word));
		string x[101];
		for(int i=0;i<n;++i){
			cin>>x[i];
			int p=0;
			for(int j=1;j<x[i].length();++j){
				if(x[i][j]!=x[i][0]){
					p=1;
					break;}}
			if(p==0)
				wo[x[i][0]-'a']=1;
			int c1='0',c2='0';
			int n1=0,n2=0,g=0;
			for(int j=0;j<x[i].length();++j){
				if(c1=='0')
					c1=x[i][j];
				else if(c1!='0'&&c2=='0'&&x[i][j]!=c1)
					c2=x[i][j];
				else if(x[i][j]!=c1&&x[i][j]!=c2){
					g=1;
					break;}
				if(x[i][j]==c1)
					n1++;
				else if(x[i][j]==c2)
					n2++;}
			if(g==0&&c2!=0&&(n1==1||n2==1)){
				word[c2-'a'][c1-'a']=1;
				word[c1-'a'][c2-'a']=1;}}
		int flag=0;
		for(int i=0;i<m;++i){
			if(wo[i]==0){
				for(int j=0;j<m;++j){
					if(word[i][j]==0){
						flag=1;
						break;}}}}
		if(flag)
			printf("Yes\n");
		else
			printf("No\n");}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值