hdu 3068 最长回文 manacher算法

模板题,不多说

代码:

var
  s1:ansistring;
  s:array[0..230000] of char;
  n,i,ans,po,mx:longint;
  len:array[0..230000] of longint;

function min(x,y:longint):longint;
begin
  if x<y then exit(x)
         else exit(y);
end;

begin
  while not eof do
  begin
    readln(s1);
    readln; 
    n:=length(s1);
    s[1]:='@';
    for i:=1 to n do
    begin
      s[i*2]:=s1[i];
      s[i*2+1]:='@';
    end;
    s[0]:='!';
    ans:=0;
    mx:=0;
    po:=0;
    for i:=1 to n*2+1 do
    begin
      if mx>i
        then len[i]:=min(len[2*po-i],mx-i)
        else len[i]:=1;
      while s[i-len[i]]=s[i+len[i]] do inc(len[i]);
      if len[i]+i>mx then
      begin
        mx:=len[i]+i;
        po:=i;
      end;
      if len[i]>ans then ans:=len[i];
    end;
    writeln(ans-1);
  end;
end.


你可能感兴趣的:(hdu 3068 最长回文 manacher算法)