Zoj 3818 Pretty Poem (brute force processing string), Mudanjiang division network competition, 2014, zoj3818

Source: Internet
Author: User

Zoj 3818 Pretty Poem (brute force processing string), Mudanjiang division network competition, 2014, zoj3818

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 too 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 poemPrettyIf it can be decomposed into one of the following rhyme scheme: "ABABA" or "ABABCAB". The symbolA,BAndCAre different continuous non-empty substrings of the poem. By the way, punctuation characters shoshould 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 integerTIndicating the number of test cases. For each test case:

There is a line of poemS(1 <= length (S) <= 50 ).SWill 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
3niconiconi~pettan,pettan,tsurupettanwafuwafu
Sample Output
YesYesNo
 
A string of no more than 50 characters is provided to determine whether it is a pretty string (ignore punctuation marks ). The pretty string is defined as follows: if A string can be written in the form of ABABA or abababcab, and A, B, and C are not equal to each other, the string is A pretty string.
Analysis: because the length does not exceed 50, you can enumerate the length of A and B and then solve the problem. During the competition, I enumerated the overall length of AB, but I don't know why WA is always there. When getting a substring, you can replace the loop with substr.
#include<iostream>#include<string>using namespace std;bool is_pretty1(string s){    if(s.length() < 5) return false;    for(int lenA = 1; lenA <= (s.length() - 2) / 3; lenA++) {        string A = s.substr(0, lenA);        for(int lenB = 1; lenA * 3 + lenB * 2 <= s.length(); lenB++) {            if(lenA * 3 + lenB * 2 != s.length()) continue;            string B = s.substr(lenA, lenB);            if(A != B) {                string AA = s.substr(lenA + lenB, lenA);                string BB = s.substr(lenA * 2 + lenB, lenB);                string AAA = s.substr(lenA * 2 + lenB * 2, lenA);                if(A == AA && A == AAA && B == BB)                    return true;            }        }    }    return false;}bool is_pretty2(string s){    if(s.length() < 7) return false;    for(int lenA = 1; lenA <= (s.length() - 4) / 3; lenA++) {        string A = s.substr(0, lenA);        for(int lenB = 1; 3 * lenA + 3 * lenB < s.length(); lenB++) {            string B = s.substr(lenA, lenB);            if(A != B) {                int lenC = s.length() - 3 * lenA - 3 * lenB;                string AA = s.substr(lenA + lenB, lenA);                string BB = s.substr(lenA * 2 + lenB, lenB);                string C = s.substr(lenA * 2 + lenB * 2, lenC);                string AAA = s.substr(lenA * 2 + lenB * 2 + lenC, lenA);                string BBB = s.substr(lenA * 3 + lenB * 2 + lenC, lenB);                if(A == AA && A == AAA && B == BB && B == BBB && A != C && B != C)                    return true;            }        }    }    return false;}bool is_pretty(string s){    return is_pretty1(s) || is_pretty2(s);}int main(){    int T;    string tmp;    cin >> T;    while(T--) {        cin >> tmp;        string s = "";        for(int i = 0; i < tmp.length(); i++)            if(isalpha(tmp[i]))                s += tmp[i];        if(is_pretty(s)) cout << "Yes" << endl;        else cout << "No" << endl;    }    return 0;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.