Codeforces 442B Kolya and Tandem Repeat(暴力)

版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/u011328934/article/details/35878073

题目连接:Codeforces 442B Kolya and Tandem Repeat

题目大意:给出一个字符串,能够再加入n个字符,问说能够找到SS的子串形式,S尽量长。

解题思路:枚举长度和起点推断就可以。超过len的能够作为随意值,可是超过len+n就不行了。

#include <cstdio>
#include <cstring>

const int N = 205;

int n, len;
char s[N];

bool judge (int l) {

    if (l <= n)
        return true;

    for (int i = 0; i < len-l+n; i++) {

        bool flag = true;

        for (int j = 0; j < l; j++) {

            if (i + j + l >= len + n) {
                flag = false;
                break;
            }

            if (i + j >= len || i + j + l >= len)
                continue;

            if (s[i+j] == s[i+j+l])
                continue;

            flag = false;
            break;
        }

        if (flag)
            return true;
    }
    return false;
}

int main () {
    scanf("%s%d", s, &n);

    len = strlen(s);

    if (n >= len) {
        printf("%d\n", (n + len) / 2 * 2);
    } else {
        for (int i = len; i >= 0; i--) {
            if (judge(i)) {
                printf("%d\n", i*2);
                break;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mqxnongmin/p/10851154.html