ZOJ3818-Pretty poem (brute force enumeration)

Source: Internet
Author: User

Question Link


Question: Check whether the given string conforms to the Ababa or ababcab format. If yes, output yes. If no, no.

Idea: enumerate the lengths of A and B in a brute force mode. Then, subtract the length of AB three times from the length, that is, the length of C, to check whether the combined string is equal to the given one. Here, the substr function in string is a good thing.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>using namespace std;char str[55];int main() {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%s", str);         string s;        int len = strlen(str);        for (int i = 0; i < len; i++)            if (isupper(str[i]) || islower(str[i]))                s += str[i];        len = s.length();        int flag = 0;        for (int i = 1; i < len / 2 && !flag; i++) {            for (int j = 1; j < len / 2 && !flag; 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(2 * (i + j), len - (i + j) * 3);                     if (A == C || B == C)                        continue;                    if (AB + AB + C + AB == s) {                        flag = 1;                         break;                     }                }             }         }         if (flag)            printf("Yes\n");        else            printf("No\n");     }    return 0;}


ZOJ3818-Pretty poem (brute force enumeration)

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.