zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Sliding Window
    方程的解数
    [JLOI2011]不重复数字
    A−B数对
    2007年分区联赛提高组之一 统计数字
    Magic Squares 魔板 (BFS+HASH)
    集合(normal)
    Place the Robots
    LoadRunner监控Linux
    CentOS6.3(64位)下安装Oracle11gR2(64)服务器
  • 原文地址:https://www.cnblogs.com/hanbinggan/p/4678844.html
Copyright © 2011-2022 走看看