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

本文探讨了如何在有限字符集下构建无限长度字符串,确保字符串中不存在禁止的子串,并且不同位置的子串不相同。通过输入描述的案例,实现了一种算法来判断是否能构建满足条件的字符串。
639

被折叠的 条评论
为什么被折叠?



