码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 442B Kolya and Tandem Repeat(暴力)

时间:2014-06-30 20:21:48      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   for   rgb   

题目连接: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;
}

Codeforces 442B Kolya and Tandem Repeat(暴力),布布扣,bubuko.com

Codeforces 442B Kolya and Tandem Repeat(暴力)

标签:style   http   color   os   for   rgb   

原文地址:http://blog.csdn.net/keshuai19940722/article/details/35878073

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!