manachar

· · 个人记录

#include <cstdio>
#include <iostream>
#define N 11000005 
using namespace std;

char str[N << 1];
int p[N << 1], top = 2, ans;

void init() {
    str[0] = '$';
    str[1] = '#';
    char ls;
    while((ls = getchar()) != -1) {
        str[top++] = ls;
        str[top++] = '#'; 
    } str[top] = '\0';
}

void manachar() {
    int mx = 0, id = 0;
    for(int i = 1;i < top;i++) {
        p[i] = (mx > i) ? 
            min(p[(id << 1) - i], mx - i) : 0;
        while(str[i - p[i] - 1] == str[i + p[i] + 1]) p[i]++;
        if(p[i] + i > mx)  mx = p[i] + i, id = i;
        if(p[i] > ans) ans = p[i];
    }
}

int main() {
    init();
    manachar();
    printf("%d", ans);
    return 0;
}