您的位置:首页 > 其它

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

2015-04-16 20:31 302 查看
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:

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.
There does not exist such (i, j) that stri..j is a banned word in S (1 <= i <= j < ∞).
There does not exist such (i, j) that for any k >= i, strk = str(j + k) (1 <= i, j < ∞).


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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: