zoj 3818 Pretty Poem(暴力枚举)

223 篇文章 1 订阅
33 篇文章 0 订阅

题目链接:zoj 3818 Pretty Poem

题目大意:给定一个字符串,忽略标点符号,考虑是否押韵,即为ABABA或者ABABCAB形式。

解题思路:暴力枚举A,B的长度,判断即可, 注意A,B,C非空,并且不相同。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
char s[105];

bool check (int a, int b, int l) {
    for (int i = 0; i < l; i++)
        if (s[a + i] != s[b + i])
            return false;
    return true;
}

bool judge (int l, int r, int flag) {
    int a = l + r;
    if (flag == 1) {

        if (!check(0, a, l) || !check(0, a*2, l))
            return false;

        if (!check(l, l + a, r))
            return false;
    } else {
        int c = n - 3 * a;
        if (!check(0, a, a) || !check(0, a*2+c, a))
            return false;

        if (l == c && check(0, 2*a, c))
            return false;

        if (r == c && check(l, 2*a, c))
            return false;
    }

    if (l == r && l == 1 && s[0] == s[1])
        return false;
    return true;
}

bool solve () {

    scanf("%s", s);
    n = 0;
    int len = strlen(s);
    for (int i = 0; i < len; i++)
        if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
            s[n++] = s[i];
    s[n] = '\0';

    for (int i = 1; i < n; i++) {
        for (int j = 1; j < n; j++) {

            if (3 * i + 2 * j == n && judge(i, j, 1))
                return true;

            if (3 * (i + j) < n && judge(i, j, 2))
                return true;
        }
    }
    return false;
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        printf("%s\n", solve() ? "Yes" : "No");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值