標籤:
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 最長雙迴文串