POJ3974 Palindrome

题意很简单、、给定一个串求最长回文子串、、

然后就是一个裸的好似叫manachure算法的东西、、

用O(n)的复杂度求最长回文子串、、

 

Code:

var
  s:ansistring;
  a:array [0..2000100] of char;
  p:array [0..2000100] of longint;
  cur,en,ans,len,vv,n,i,id:longint;
function min(a,b:longint):longint;
  begin
    if a<b then exit(a) else exit(b);
  end;
begin
  readln(s);
  while s<>'END' do
    begin
      len:=length(s);n:=0;inc(vv);
      for i:=1 to len do
        begin
          inc(n);a[n]:=s[i];
          inc(n);a[n]:='#';
        end;
      fillchar(p,sizeof(p),0);
      a[n]:='$';
      en:=0;ans:=0;
      for i:=1 to n-1 do
        begin
          if en>i then
            p[i]:=min(p[id*2-i],en-i)
          else p[i]:=1;
          while (i>p[i]) and (a[i-p[i]]=a[i+p[i]]) do inc(p[i]);
          dec(p[i]);
          if i+p[i]>en then
            begin
              en:=i+p[i];
              id:=i;
            end;
          if i and 1=1
            then cur:=p[i] div 2*2+1
            else cur:=(p[i]+1) div 2*2;
          if cur>ans then ans:=cur;
        end;
      writeln('Case ',vv,': ',ans);
      readln(s);
    end;
end.

  

posted @ 2013-05-20 11:02  JS_Shining  阅读(406)  评论(0编辑  收藏  举报