网络流就先告一段落了
在进行其他训练之前,我决定先练一道后缀数组(对这个我还是比较有自信的)
虽然之前没用后缀数组解决过回文问题,但是稍微想想就知道,
要解决最长双倍回文,首先要解决最长回文序列,
要解决最长回文序列,首先要倒序添加原串然后LCP
任意两个后缀的LCP我就不多说了,
然后我们就可以求出以任意一个字符为中心展开的最长回文串(要小心,偶数长度的最长回文串)
然后就能求出每个字符向左向右延伸回文串能延伸多远,
最后在遍历一边就可以了。
var h,sa,rank,x,y,sum:array[..] of longint;
f:array[..,..] of longint;
d:array[..] of longint;
left,right:array[..] of longint;
p,i,j,l,n,m,ans,t:longint;
s:ansistring; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure suffix;
var m,p,i,j:longint;
begin
for i:= to n do
begin
y[i]:=ord(s[i]);
inc(sum[y[i]]);
end;
m:=;
for i:= to m do
inc(sum[i],sum[i-]);
for i:=n downto do
begin
sa[sum[y[i]]]:=i;
dec(sum[y[i]]);
end;
p:=;
rank[sa[]]:=;
for i:= to n do
begin
if y[sa[i]]<>y[sa[i-]] then inc(p);
rank[sa[i]]:=p;
end;
m:=p;
j:=;
while m<n do
begin
y:=rank;
fillchar(sum,sizeof(sum),);
p:=;
for i:=n-j+ to n do
begin
inc(p);
x[p]:=i;
end;
for i:= to n do
if sa[i]>j then
begin
inc(p);
x[p]:=sa[i]-j;
end;
for i:= to n do
begin
rank[i]:=y[x[i]];
inc(sum[rank[i]]);
end;
for i:= to m do
inc(sum[i],sum[i-]);
for i:=n downto do
begin
sa[sum[rank[i]]]:=x[i];
dec(sum[rank[i]]);
end;
p:=;
rank[sa[]]:=;
for i:= to n do
begin
if (y[sa[i]]<>y[sa[i-]]) or (y[sa[i]+j]<>y[sa[i-]+j]) then inc(p);
rank[sa[i]]:=p;
end;
m:=p;
j:=j shl ;
end;
h[]:=;
p:=;
for i:= to n do
begin
if rank[i]= then continue;
j:=sa[rank[i]-];
while s[i+p]=s[j+p] do inc(p);
h[rank[i]]:=p;
if p> then dec(p);
end;
end; procedure rmq;
begin
t:=trunc(ln(n)/ln());
d[]:=;
for i:= to t do
d[i]:=d[i-]*; for i:= to n do
f[i,]:=h[i];
for j:= to t do
for i:= to n do
if (i+d[j]-<=n) then
f[i,j]:=min(f[i,j-],f[i+d[j-],j-]);
end; function ask(x,y:longint):longint;
var k:longint;
begin
if x>y then swap(x,y);
inc(x);
k:=trunc(ln(y-x+)/ln());
ask:=min(f[x,k],f[y-d[k]+,k]);
end; begin
readln(s);
l:=length(s);
s:=s+'*';
for i:=l downto do
begin
s:=s+s[i];
left[i]:=;
right[i]:=;
end;
n:=length(s);
suffix;
rmq;
for i:= to l do
begin
p:=ask(rank[i],rank[n+-i]); //先求奇数长度的回文序列
left[i-p+]:=max(left[i-p+],p*-);
right[i+p-]:=max(right[i+p-],p*-);
if i<>l then
begin
p:=ask(rank[i],rank[n-i]); //偶数长度的回文序列
if p> then dec(p); //细节
if p<> then
begin
left[i-p+]:=max(left[i-p+],*p);
right[i+p]:=max(right[i+p],*p);
end;
end;
end;
for i:=l- downto do //处理每个字符为回文串的一端的最远延伸
right[i]:=max(right[i],right[i+]-);
for i:= to l do
left[i]:=max(left[i],left[i-]-);
for i:= to l- do //不难理解
ans:=max(ans,right[i]+left[i+]);
writeln(ans);
end.