• 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;
    }
  • 相关阅读:
    P1144 最短路计数
    P2966 [USACO09DEC]牛收费路径Cow Toll Paths
    P2419 [USACO08JAN]牛大赛Cow Contest
    P1462 通往奥格瑞玛的道路
    P1346 电车
    P1339 [USACO09OCT]热浪Heat Wave
    P1418 选点问题
    P1330 封锁阳光大学
    P1182 数列分段Section II
    P2661 信息传递
  • 原文地址:https://www.cnblogs.com/hanbinggan/p/4678844.html
Copyright © 2020-2023  润新知