Pretty Poem
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 Input3
niconiconi~
pettan,pettan,tsurupettan
wafuwafu
Sample OutputYes
Yes
No
意义大致是要求在长度不足50的字符串中找出子串A,B,C使其整个串满足ABABA或ABABCAB的形式且A!=B!=C
#include <stdio.h>
#include <string.h>
char s[55],t,s_a[20]={'\0'},s_b[20]="\0",s_c[20]="\0";
int a,b,c,flag=0;
int cmp(char *a,char *b)//判断是否相等
{
int i=0,len1=strlen(a),len2=strlen(b);
if (len1!=len2) return 0;
while (i<len1){
if (a[i]!=b[i])
return 0;
i++;
}
return 1;
}
void dfs(int k)//暴搜
{
int i,j,len=strlen(s),tt[5]={1,0,1,0,1},x[7]={1,0,1,0,0,1,0};
char s_f[55]="\0";
if (k==3&&3*strlen(s_a)+2*strlen(s_b)==len&&!cmp(s_a,s_b)){//ABABA情况的拼接形式
for (i=0;i<5;i++){
if (tt[i]){
strcat(s_f,s_a);
}
else{
strcat(s_f,s_b);
}
}
if (cmp(s_f,s)){
flag=1;
return ;
}
}
if (k==4&&3*strlen(s_a)+3*strlen(s_b)+strlen(s_c)==len&&!cmp(s_a,s_b)&&!cmp(s_a,s_c)&&!cmp(s_b,s_c)){//ABABCAB情况的拼接。
for (i=0;i<7;i++){
if (i==4){
strcat(s_f,s_c);
continue;
}
if (x[i]){
strcat(s_f,s_a);
}
else{
strcat(s_f,s_b);
}
}
if (cmp(s_f,s)){
flag=1;
return ;
}
}
if (k==1&&!flag){//分三层枚举,b和c的初始值取值需要仔细设计,if(!flag)作用为剪枝。
memset(s_a,0,sizeof s_a);
for (a=0;a<len/3;a++){
s_a[a]=s[a];
dfs(k+1);
}
}
if (k==2&&!flag){
memset(s_b,0,sizeof s_b);
for (b=a+1,j=0;b<len/2;j++,b++){
s_b[j]=s[b];
dfs(k+1);
}
}
if (k==3&&!flag){
memset(s_c,0,sizeof s_c);
for (c=2*b+2,j=0;c<len-1;j++,c++){//其实a,b,c的范围限定都是一个模糊的限制,但是if c<len.这里当c=len-1的时候a突然变成110导致枚举失败,原因未知。
s_c[j]=s[c];
// printf ("a=%s\nb=%s\nc=%s\n",s_a,s_b,s_c); //这条语句可以将枚举过程打印出来有助于理解枚举过程。
dfs(k+1);
}
}
}
int main ()
{
int n;
scanf ("%d",&n);
getchar ();
while (n--){
int p=0;
while (1){
t=getchar ();
if (t<='z'&&t>='a'||t<='Z'&&t>='A'){//提交之后WA了两发,原因就是没有考虑字符串中可以包含大写字母。
s[p++]=t;
}
if (t=='\n') break;
}
dfs(1);
if (flag) printf ("Yes\n");
else printf ("No\n");
flag=0;
memset(s,0,sizeof s);
}
return 0;
}
一开始看到这样子要求分割字符串的题目感觉无从下手,其实也不是很难,但是做这道题是做了好多个小时。不过针对小写字母来说是一次AC的。还是可以傲娇一下~~这道题值得仔细思考一下作为字符串分割的模板。
还有欢迎巨佬指出为什么在枚举C子串的时候发生的那个bug(见注释),bug发生在测试第二个样例的时候。