Pretty Poem
Time Limit: 2 Seconds Memory Limit: 65536 KB
Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can even write poetic code. Some poems has a strict rhyme scheme like "ABABA" or "ABABCAB". For example, "niconiconi" is composed of a rhyme scheme "ABABA" with A = "ni" and B = "co".
More technically, we call a poem pretty if it can be decomposed into one of the following rhyme scheme: "ABABA" or "ABABCAB". The symbol A, B and C are different continuous non-empty substrings of the poem. By the way, punctuation characters should be ignored when considering the rhyme scheme.
You are given a line of poem, please determine whether it is pretty or not.
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:
There is a line of poem S (1 <= length(S) <= 50). S will only contains alphabet characters or punctuation characters.
Output
For each test case, output "Yes" if the poem is pretty, or "No" if not.
Sample Input
3 niconiconi~ pettan,pettan,tsurupettan wafuwafu
Sample Output
Yes Yes No
1 /* 2 许杰浩的题解: 3 AC代码: 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <string.h> 8 #include <cstring> 9 using namespace std; 10 char str[55],s1[55]; 11 char s2[55],s3[55],s4[55]; 12 int cnt; 13 void getap(){ 14 cnt=1; 15 int l=strlen(str); 16 for(int i=0;i<l;i++){ 17 if(str[i]<=‘z‘&&str[i]>=‘a‘){ 18 s1[cnt++]=str[i]; 19 } 20 else if(str[i]<=‘Z‘&&str[i]>=‘A‘){ 21 s1[cnt++]=str[i]; 22 } 23 } 24 } 25 void getstr(int i,int j,char *s){ 26 int p=0; 27 for(int k=0;k<55;k++)s[k]=‘\0‘; 28 for(int k=i;k<=j;k++){ 29 s[p++]=s1[k]; 30 } 31 } 32 void print(int l,int k){ 33 for(int i=l;i<=k;i++){ 34 printf("%c",s1[i]); 35 } 36 cout<<endl; 37 } 38 bool solve1(){ 39 int len=cnt-1; 40 if(len<5)return false; 41 for(int i=1;i<=len;i++){ 42 if(s1[i]!=s1[len])continue; 43 if(len-i*3<2)break; 44 if((len-i*3)%2==1)continue; 45 int l=(len-i*3)/2; 46 bool flag=1; 47 for(int j=1;j<=i;j++){ 48 if(s1[j]!=s1[j+l+i]){flag=0;break;} //ababa 1a 2a 49 if(s1[j]!=s1[len-i+j]){flag=0;break;} //ababa 1a 3a 50 } 51 //print(1,i); 52 //print(i+1,i+l); 53 if(!flag)continue; 54 if(l==i){ //ababa a b 55 bool ok1=0; 56 for(int j=1;j<=i;j++){ 57 if(s1[j]!=s1[j+i]){ok1=1;break;} 58 } 59 if(!ok1)flag=0; 60 } 61 if(!flag)continue; 62 for(int j=1;j<=l;j++){ //ababa 1b 2b 63 if(s1[j+i]!=s1[j+i*2+l]){flag=0;break;} 64 } 65 if(flag)return true; 66 } 67 return false; 68 } 69 bool solve2(){ 70 int len=cnt-1; 71 if(len<7)return false; 72 for(int i=2;i<=len;i++){ 73 if(s1[i]!=s1[len])continue; 74 if(len-i*3<1)break; 75 bool flag=1; 76 //print(1,i); 77 for(int j=1;j<=i;j++){ //ababcab 78 if(s1[j]!=s1[len-i+j]){flag=0;break;} //ababcab 1ab 3ab 79 if(s1[j]!=s1[i+j]){flag=0;break;} //ababcab 1ab 2ab 80 } 81 if(!flag)continue; 82 getstr(2*i+1,len-i,s4); 83 //print(2*i+1,len-i); 84 for(int j=1;j<i;j++){ 85 getstr(1,j,s2); 86 getstr(j+1,i,s3); 87 if(strcmp(s2,s3)!=0&&strcmp(s4,s2)!=0&&strcmp(s3,s4)!=0){ //a!=b c!=a b!=c 88 //print(1,j);print(j+1,i); 89 return true; 90 } 91 } 92 } 93 return false; 94 } 95 96 int main(){ 97 int t; 98 scanf("%d",&t); 99 while(t--){ 100 scanf("%s",str); 101 getap(); 102 if(solve1()||solve2())printf("Yes\n"); 103 else printf("No\n"); 104 } 105 }