manacher --- 暂 旧版本

  

#include<bits/stdc++.h>
using namespace std;
const int N = 110010;
char str[N], aim[N<<1];
int n;
int len[N*2];
void manacher()
{
    memset(len, 0, sizeof(len));
    aim[0] = '$'; aim[1] = '#';
    int Maxid = 0;
    int id = 0;
    for(int i = 0, j = 2; i < n; i++){
        aim[j++] = str[i];
        aim[j++] = '#';
    }
    for(int i = 1; i <= n*2+1; i++)
    {
        if(i < Maxid)
            len[i] = min(len[id*2-i], Maxid-i);
        else len[i] = 1;
        while(aim[i-len[i]] == aim[i+len[i]]) len[i]++;
        if(i+len[i] > Maxid)
        {
            Maxid = i+len[i];
            id = i;
        }
    }
}
int main()
{
    while(~scanf("%s", str))
    {
        n = strlen(str);
        manacher();
        int ans = 0;
        for(int i = 1; i <= n*2+1; i++)
            ans = max(ans, len[i]);
        cout << ans - 1 << endl;
    }
    return 0;
}
View Code

 

posted @ 2019-07-15 19:51  Schenker  阅读(167)  评论(0编辑  收藏  举报