0
点赞
收藏
分享

微信扫一扫

POJ - 3974 Palindrome(manacher)


题目大意:问一个字符串的最大回文子串

解题思路:manacher裸题了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2000010;

char str[N];
int p[N];
int len, cas = 1;

void init () {
    len = strlen(str);
    for (int i = len; i >= 0; i--) {
        str[i * 2 + 2] = str[i];
        str[i * 2 + 1] = '#';
    }
    str[0] = '$';
}

void solve() {
    int id, mx = 0, ans = 0;
    for (int i = 2; i < 2 * len - 1; i++) {
        if (mx > i) p[i] = min(p[id * 2 - i], mx - i);
        else p[i] = 1;

        while (str[i - p[i]] == str[i + p[i]]) p[i]++;
        if (i + p[i] >= mx) {
            mx = i + p[i];
            id = i;
        }
        if (p[i] > ans) ans = p[i];
    }
    printf("Case %d: %d\n", cas++, ans - 1);

}

int main() {
    while (scanf("%s", str) != EOF && strcmp(str, "END") != 0) {
        init();
        solve();

    }
    return 0;
}


举报

相关推荐

0 条评论