题意:求一个由小写字母组成的字符串中的最长回文长度
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.
时间: 2024-10-02 18:56:47