Cod sursa(job #743700)

Utilizator RadioactivMihai Preguza Radioactiv Data 5 mai 2012 15:37:23
Problema Potrivirea sirurilor Scor 24
Compilator fpc Status done
Runda Arhiva educationala Marime 1.14 kb
var h,hr,n,m,r,i,j:longword;
    a,b:array[1..2000000] of char;
    buf,bf:array[1..1 shl 17] of char;
    c:array[1..2000000] of longword;
    z:boolean;

function valid(x:longword):boolean;
Begin
 z:=true;
 for j:=1 to m do
    if a[j]<>b[j+x] then z:=false;
  valid:=z;
End;


BEGIN
  assign(input,'strmatch.in');
  settextbuf(input,buf);
  reset(input);
  m:=0;
  n:=0;
  while not eoln do
    begin
      m:=m+1;
      read(a[m]);
    end;
  readln;
  while not eoln do
    begin
      n:=n+1;
      read(b[n]);
    end;
  close(input);
  h:=0;
  r:=0;
  hr:=0;
  for i:=1 to m do
    h:=h+ord(a[i]);
  for i:=1 to m do
    hr:=hr+ord(b[i]);
  if valid(1) then
    begin
      c[1]:=1;
      r:=1;
    end;
  for i:=1 to n-m+1 do
    begin
      hr:=hr-ord(b[i])+ord(b[i+m]);
      if hr=h then
        if valid(i)
          then
            begin
              r:=r+1;
              c[r]:=i;
            end;
    end;
  assign(output,'strmatch.out');
  settextbuf(output,bf);
  rewrite(output);
  writeln(r);
  if r>1000 then r:=1000;
  for i:=1 to r do
    write(c[i], ' ');
  close(output);
END.