hihocoder1302 最长回文子串

hihocoder1302 最长回文子串

先贴代码

所有的上面的提示已经交代的好清楚了……

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
#include <climits>
using namespace std;
typedef long long ll;
const double ESP = 10e-8;
const int MOD = 1000000000+7;
const int MAXN = 1000000+10;

char Str[MAXN];
char str[MAXN<<1];
int f[MAXN<<1];

int main(){
//    freopen("input.txt","r",stdin);
//    ios::sync_with_stdio(false);
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",Str);
        int len = strlen(Str);
        int n = 0;
        str[n++] = '$';
        str[n++] = '#';
        for(int i = 0;i < len;i++){
            str[n++] = Str[i];
            str[n++] = '#';
        }
        //f[i]真正的回文子串长度+1,也是当前回文串的一半长度+1
        str[n] = '';
        int ans = 0;
        int mx = 0; //不是回文子串的下一个位置
        int j;
        for(int i = 0;i < n;i++){
            if(mx > i){
                f[i]=min(mx-i,f[2*j-i]);
            }else{
                f[i] = 1;
            }
            while(str[i-f[i] ]==str[i+f[i] ]){
                f[i]++;
            }
            if(f[i]+i > mx){
                mx = f[i]+i;
                j = i;
            }
            ans = max(f[i]-1,ans);

        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hanbinggan/p/4678844.html