首页 > 代码库 > 【HDOJ3068】最长回文(manacher)

【HDOJ3068】最长回文(manacher)

题意:求一个由小写字母组成的字符串中的最长回文长度

cas<=120 n<=110000

思路:试manacher板子

 1 var a:array[0..300000]of char;
 2     p:array[0..300000]of longint;
 3     ch:ansistring;
 4     len,n,i,mx,id,ans:longint;
 5 
 6 function min(x,y:longint):longint;
 7 begin
 8  if x<y then exit(x);
 9  exit(y);
10 end;
11 
12 function max(x,y:longint):longint;
13 begin
14  if x>y then exit(x);
15  exit(y);
16 end;
17 
18 begin
19  assign(input,hdoj3068.in); reset(input);
20  assign(output,hdoj3068.out); rewrite(output);
21  while not eof do
22  begin
23   readln(ch);
24   len:=length(ch);
25   if len=0 then continue;
26   n:=2; a[1]:=@; a[2]:=#;
27   for i:=1 to len do
28   begin
29    inc(n); a[n]:=ch[i];
30    inc(n); a[n]:=#;
31   end;
32   inc(n); a[n]:=$;
33   mx:=0; id:=0;
34   for i:=2 to n-1 do
35   begin
36    if mx>i then p[i]:=min(p[2*id-i],mx-i)
37     else p[i]:=1;
38    while a[i+p[i]]=a[i-p[i]] do inc(p[i]);
39    if p[i]+i>mx then
40    begin
41     mx:=p[i]+i;
42     id:=i;
43    end;
44   end;
45   ans:=1;
46   for i:=2 to n-1 do ans:=max(ans,p[i]-1);
47   writeln(ans);
48  end;
49  close(input);
50  close(output);
51 end.

 

【HDOJ3068】最长回文(manacher)