标签:
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.
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.
For each test case, output "Yes" if the poem is pretty, or "No" if not.
3 niconiconi~ pettan,pettan,tsurupettan wafuwafu
Yes Yes No
题目大意:
这道题目是说,给你一个长度不超过55的文章,要求你把其中的类似与ABABA和ABABCAB的格式串找出来,如果能够找到这样的串,就输出Yes、
解题思路:
这道题是自己关于substr(i,j)函数的学习,substr(i,j)指的是,从str[i]开始的长度为j的字符串。刚刚好,我们枚举substr(0,i)作为A
substr(i,j)作为B,那么A的长度就是i,B的长度就是j,将这两个字符串进行拼接组合,看能否得到一个我们需要满足的字符串的形式。
关于对于C的枚举,我们可以用len-(i+j)*3得到C的长度然后通过substr((i+j)*2, len-(i+j)*3 ).
代码:
# include<cstdio> # include<iostream> # include<cstring> # include<string> using namespace std; # define MAX 55 char str[MAX]; string s; int main(void) { int t; scanf("%d",&t); while ( t-- ) { scanf("%s",str); int len = strlen(str); for ( int i = 0;i < len;i++ ) { if ( islower(str[i])||isupper(str[i]) ) s+=str[i]; } //cout<<s<<endl; len = s.size(); int flag = 0; for ( int i = 1;i < len&&flag==0;i++ ) { for ( int j = 1;j < len&&flag==0;j++ ) { string A = s.substr(0,i); string B = s.substr(i,j); if ( A==B ) continue; if ( A+B+A+B+A==s ) { flag = 1; break; } if ( len-(i+j)*3>0 ) { string AB = A+B; string C = s.substr((i+j)*2,len-(i+j)*3); if ( A==C||B==C ) continue; if ( AB+AB+C+AB==s ) { flag = 1; break; } } } } if (flag) puts("Yes"); else puts("No"); s.clear(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/wikioibai/p/4794950.html