zoj 3818 Pretty Poem(暴力處理字串)2014年牡丹江賽區網路賽,zoj3818

來源:互聯網
上載者:User

zoj 3818 Pretty Poem(暴力處理字串)2014年牡丹江賽區網路賽,zoj3818

Pretty PoemTime 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 AB 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
3niconiconi~pettan,pettan,tsurupettanwafuwafu
Sample Output
YesYesNo

題意:給出一個長度不超過50的字串,判斷它是不是pretty串(忽略標點符號)。pretty串的定義如下:如果一個串可以寫成ABABA或者ABABCAB的形式,且A、B、C兩兩不相等,那麼這個串就是pretty串。
分析:因為長度不超過50,所以可以直接枚舉A、B的長度,然後暴力求解。比賽時我枚舉的是AB整體的長度,不知道為什麼一直WA。取子串時,可以用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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.