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 is many famous poets in the contemporary era. It is said a few ACM-ICPC contestants can even write poetic code. Some poems have a strict rhyme scheme like "Ababa" or "Ababcab". For example, "Niconiconi" was composed of a rhyme scheme "ababa" with a = "ni" and B = "CO".
More technically, we call a poem pretty if it can being decomposed into one of the following rhyme scheme: "Ababa" O R "Ababcab". The symbol A, B and C are different continuous non-empty substrings of the poem. By the punctuation characters should is ignored when considering the rhyme scheme.
You were given a line of poem, please determine whether it was pretty or not.
Input
There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:
There is a line of poem S (1 <= Length ( S ) <=). Would only contains alphabet characters S or punctuatio n characters.
Output
For each test case, the output "Yes" if the poem is pretty, or "No" if not.
Sample Input
3niconiconi~pettan,pettan,tsurupettanwafuwafu
Sample Output
Yesyesno
Main topic:
This topic is said, give you a length of not more than 55 of the article, asked you to the similar with Ababa and Ababcab format string to find out, if you can find such a string, output Yes,
Problem Solving Ideas:
This question is my own study of substr (i,j) function, substr (I,J) refers to a string of length J, starting with Str[i]. Just fine, we enumerate substr (0,i) as a
SUBSTR (i,j) as B, then the length of a is the length of the I,b is J, the two strings are stitched together to see if we can get a string to satisfy the form.
For the enumeration of C, we can use len-(i+j) to get the length of C and then pass substr (I+j), len-(I+J).
Code:
# include<cstdio># include<iostream># include<cstring># include<string>using namespacestd;# define MAX -CharStr[max];strings;intMainvoid){ intT scanf"%d",&t); while(t--) {scanf ("%s", str); intLen =strlen (str); for(inti =0; I < len;i++ ) { if(Islower (str[i]) | |Isupper (Str[i])) s+=Str[i]; } //cout<<s<<endl;Len =s.size (); intFlag =0; for(inti =1; I < len&&flag==0; i++ ) { for(intj =1; J < len&&flag==0; j + + ) { stringA = S.substr (0, i); stringB =s.substr (I,J); if(a==B)Continue; if(a+b+a+b+a==s) {flag=1; Break; } if(len-(I+J) *3>0 ) { stringAB = A +C; stringC = 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"); Elseputs ("No"); S.clear (); } return 0;}
ZOJ 3818 (use of substr function)