Cod sursa(job #170849)

Utilizator gurneySachelarie Bogdan gurney Data 3 aprilie 2008 12:43:02
Problema PScPld Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.55 kb
program pscpld;
  const
    fin='pscpld.in';
    fout='pscpld.out';
    nmax=1000003;
type
  chararray=array[0..nmax] of char;
  pc=^chararray;
  intarray=array[0..2*nmax] of longint;
  pi=^intarray;
var
  a:pc;
  lg,pal:pi;
  s,i,j,k,n,st,dr,p,stp,drp,p1,p2:longint;

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

function det_lg(y:longint):longint;
  begin
    if st=dr then
      det_lg:=(st-(y+3) shr 1+1)shl 1-1
    else
      det_lg:=(st-(y+3) shr 1+1)shl 1;
  end;

function det2_lg:longint;
  begin
    if st=dr then
      det2_lg:=(drp-st+1)shl 1-1
    else
      det2_lg:=(drp-dr+1)shl 1;
  end;

begin
  assign(input,fin);
    reset(input);
    n:=0;
    new(a);
    a^[0]:='#';
    while not(eof(input)) do
      begin
        inc(n);
        read(a^[n]);
      end;
    a^[n+1]:='$';
  close(input);
  assign(output,fout);
    rewrite(output);
    new(pal);new(lg);
    fillchar(pal^,sizeof(pal^),0);
    s:=0;
    for i:=1 to (n shl 1)-1 do
      begin
        if i and 1=1 then
          begin
            st:=(i shr 1)+1;
            dr:=st;
          end
        else
          begin
            st:=i shr 1;
            dr:=st+1;
          end;
        if (a^[st]=a^[dr]) then
          begin
            if (pal^[i+1]=0)and(i and 1=0) then
                  pal^[i+1]:=i;
            if pal^[i]<>0 then
              begin
                if pal^[i] and 1=0 then
                  drp:=pal^[i] shr 1+lg^[pal^[i]] shr 1
                else
                  drp:=pal^[i] shr 1+1+lg^[pal^[i]]shr 1;
                p1:=det_lg(pal^[i]);
                p2:=det2_lg;
                p:=min(p1,p2);
                p:=min(p,lg^[pal^[i] shl 1-i]);
                dec(st,p shr 1);
                inc(dr,p shr 1);
                if (i and 1=0)and(dr-st>1) then
                  begin
                    inc(st);dec(dr);
                  end;
              end;
            while (a^[st-1]=a^[dr+1]) do
              begin
                if lg^[pal^[dr shl 1]]<dr-st+1 then
                  pal^[dr shl 1]:=i;
                if lg^[pal^[(dr+1)shl 1-1]]<dr-st+1 then
                  pal^[((dr+1) shl 1)-1]:=i;
                dec(st);
                inc(dr);
              end;
            lg^[i]:=dr-st+1;
            if i and 1=0 then
              inc(s,lg^[i] shr 1)
            else
              inc(s,lg^[i] shr 1+1);
          end
        else
          lg^[i]:=0;
      end;
    writeln(s);
  close(output);
end.