BZOJ2565 最長雙迴文串

來源:互聯網
上載者:User

標籤:

Description順序和逆序讀起來完全一樣的串叫做迴文串。比如acbca是迴文串,而abc不是(abc的順序為“abc”,逆序為“cba”,不相同)。
輸入長度為n的串S,求S的最長雙迴文子串T,即可將T分為兩部分X,Y,(|X|,|Y|≥1)且X和Y都是迴文串。Input

一行由小寫英文字母組成的字串S。

Output一行一個整數,表示最長雙迴文子串的長度。Sample Inputbaacaabbacabb
Sample Output12
HINT

 

範例說明

從第二個字元開始的字串aacaabbacabb可分為aacaa與bbacabb兩部分,且兩者都是迴文串。

對於100%的資料,2≤|S|≤10^5


2015.4.25新加資料一組

 

Source

2012國家集訓隊Round 1 day2

水水噠PAM,正反得到每個位置向左和向右的最長迴文串長度,再掃一遍即可
#include<cstdio>#include<cctype>#include<queue>#include<cstring>#include<algorithm>#define rep(s,t) for(int i=s;i<=t;i++)#define ren for(int i=first[x];i!=-1;i=next[i])using namespace std;inline int read() {    int x=0,f=1;char c=getchar();    for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;    for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;    return x*f;}const int maxn=100010;char ch[maxn];int f2[maxn],g2[maxn];struct PAM {    int cnt,last;    int to[maxn][26],l[maxn],f[maxn];    void init() {        memset(to,0,sizeof(to));        memset(l,0,sizeof(l));        memset(f,0,sizeof(f));        cnt=f[0]=1;l[1]=-1;last=0;    }    void extend(int c,int n,int t) {        int p=last;        while(ch[n]!=ch[n-l[p]-1]) p=f[p];        if(!to[p][c]) {            int np=++cnt,k=f[p];l[np]=l[p]+2;            while(ch[n]!=ch[n-l[k]-1]) k=f[k];            f[np]=to[k][c];to[p][c]=np;        }        last=to[p][c];        if(!t) f2[n]=l[last];        else g2[n]=l[last];    }}sol;int main() {    scanf("%s",ch+1);int n=strlen(ch+1),ans=0;    sol.init();rep(1,n) sol.extend(ch[i]-‘a‘,i,0);    sol.init();reverse(ch+1,ch+n+1);rep(1,n) sol.extend(ch[i]-‘a‘,i,1);    rep(2,n) ans=max(ans,f2[i-1]+g2[n-i+1]);    printf("%d\n",ans);    return 0;}
View Code

 

BZOJ2565 最長雙迴文串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.