manacher

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

const int N = 101000100;

char str[N];

int pos, ans, rl[N], len, maxRight;

int main() {
    scanf("%s", str);
    len = strlen(str);
    for(int i = len - 1 ; i >= 0 ; -- i)
        str[2 * i + 1] = str[i];
    len = len * 2 + 1;
    for(int i = 0 ; i < len ; ++ i)
        if(!(i & 1))
            str[i] = '#';
    for(int i = 0 ; i < len ; ++ i) {
        if(i < maxRight)
            rl[i] = min(rl[2 * pos - i], maxRight - i);
        else
            rl[i] = 1;
        while(i - rl[i] >= 0 && i + rl[i] < len && str[i - rl[i]] == str[i + rl[i]])
            ++ rl[i];
        if(i + rl[i] - 1 > maxRight) {
            maxRight = i + rl[i] - 1;
            pos = i;
        }
        ans = max(ans, rl[i]);
    }
    printf("%d\n", ans - 1);
}

  

posted @ 2017-10-07 11:20  KingSann  阅读(117)  评论(0编辑  收藏  举报