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 < ∞).
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.
For each test case, output "Yes" if it is possible to construct such a string, otherwise "No".
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; }
相关文章推荐
- AC自动机,Tarjan(ZOJ 3784,String of Infinity)
- Codeblocks编译C++出现to_string is not a member of std 或者 to_string was not declared in this scope的解决方法
- ASP.NET中String.IndexOf 方法的使用
- IOS成长之路-Nsstring中搜索方法rangeOfString
- ZOJ - 3985 - String of CCPC (分类讨论)
- C# String.IndexOf 方法
- String.IndexOf 方法(C#)
- indexOf(String.indexOf 方法)
- Java中toString方法和String.valueOf方法使用
- indexOf(String.indexOf 方法)
- js判断对象类型 typeof与Object.prototype.toString.call方法的区别
- indexOf(String.indexOf 方法)
- String.valveOF的使用方法
- 完美解决Invalid layout of java.lang.String at value问题的方法
- JS中string方法中常用方法之八:String.prototype.indexOf()
- String.IndexOf 方法
- Java String.indexOf(String str)方法
- 按字符串读取文件内容NSString stringWithContentsOfFile: (NSString类方法)
- indexOf(String.indexOf 方法)
- indexOf(String.indexOf 方法)