Cod sursa(job #170119)

Utilizator gurneySachelarie Bogdan gurney Data 2 aprilie 2008 13:36:01
Problema PScPld Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.26 kb
program pscpld;
  const
    fin='pscpld.in';
    fout='pscpld.out';
    nmax=100003;
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:longint;

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

function det_lg(x,y:longint):longint;
  begin
    if (y and 1=0) then
      if st<>dr then
        det_lg:=(st-y shr 1)shl 1
      else
        det_lg:=(st-y shr 1) shl 1-1
    else
      if st<>dr then
        det_lg:=(st-(y shr 1)) shl 1
      else
        det_lg:=((st-(y shr 1)) shl 1)-1;
  end;

begin
  assign(input,fin);
    reset(input);
    n:=0;
    new(a);
    a^[0]:='#';
    while not(seekeof(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
                p:=min(lg^[(pal^[i] shl 1)-i],det_lg(i,pal^[i]));
                dec(st,p shr 1);
                inc(dr,p shr 1);
                if i and 1=0 then
                  begin
                    inc(st);dec(dr);
                  end;
              end;
            while (a^[st-1]=a^[dr+1]) do
              begin
                if pal^[dr shl 1]=0 then
                  pal^[dr shl 1]:=i;
                if pal^[(dr+1)shl 1-1]=0 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.