zoukankan      html  css  js  c++  java
  • hihoCoder 1032

    最长回文子串的O(1)算法Manacher算法

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stack>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    #define LL long long
    const double pi = acos(-1.0);
    const int INF = 0x3f3f3f3f;
    #define exp 1e-6
    
    char str[2000010];
    char ss[2000010];
    int n;
    int p[2000010];
    
    void solve()
    {
        int i;
        int mx=0;
        int id;
        memset(p,0,sizeof(p));
        for(int i=1;i<n;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]++;
            if(p[i]+i>mx)
            {
                mx=p[i]+i;
                id=i;
            }
        }
        int ans=1;
        for(int i=1;i<n;i++)
        {
            ans=max(ans,p[i]);
        }
        printf("%d
    ",ans-1);
    }
    void init(int len)
    {
        int num=0;
        str[num++]='$';
        for(int i=0;i<len;i++)
        {
            str[num++]='#';
            str[num++]=ss[i];
        }
        str[num++]='#';
        n=num;
        /*for(int i=0;i<num;i++)
        {
            printf("%c",str[i]);
        }
        */
    }
    
    
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            scanf("%s",ss);
            int len=strlen(ss);
    
            init(len);
            solve();    
        }
    } 
  • 相关阅读:
    POJ_1485_dp
    POJ_1376_bfs
    [noi1994]海盗
    [noi1755]Trie
    [luogu3733]八纵八横
    [noi1774]array
    [noi1773]function
    [noi1754]SA
    [noi1779]D
    [bzoj4873]寿司餐厅
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934497.html
Copyright © 2011-2022 走看看