HDU 3068 [最长回文子串]

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#define MAX_LEN 310000
using namespace std;
int p[MAX_LEN];
int findBMstr(string &str){
    int maxx=-1;
    memset(p,0,sizeof(p));
    int mx=0,id=0;
    for(int i=1;i<=str.size();i++){
        if(mx>i){
            p[i]=min(p[2*id-i],mx-i);
        }
        else{
            p[i]=1;
        }
        while(str[i-p[i]]==str[i+p[i]])
            p[i]++;
        maxx=max(maxx,p[i]);
        if(i+p[i]>mx){
            mx=i+p[i];
            id=i;
        }
    }
    return maxx-1;
}
int main()
{
    string str;
    while(cin>>str){
        string str0;
        str0 +="$#";
        for(int i=0;i<str.size();i++){
            str0+=str[i];
            str0+="#";
        }
        cout<< findBMstr(str0) << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/tun117/p/5458840.html