Codeforces 442B Kolya and Tandem Repeat(暴力)

來源:互聯網
上載者:User

題目串連: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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.