Cod sursa(job #1212046)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 23 iulie 2014 18:18:14
Problema Potrivirea sirurilor Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.93 kb
program zfunction;
  var bufin,bufout:array [1..100000] of longint;
      a,b:ansistring;
      n,m,i,r,l,k:longint;
      z:array [1..4000001] of longint;

begin
  assign(input,'strmatch.in');
  reset(input);
  settextbuf(input,bufin);
  assign(output,'strmatch.out');
  rewrite(output);
  settextbuf(output,bufout);

  readln(a);
  readln(b);
  n:=length(a);
  m:=length(b);
  a:=a+'#'+b;



  r:=1;l:=1;
  for i:=2 to n+m+1 do
    begin
      if i<=r then
        if z[i-l+1]<r-i+1 then z[i]:=z[i-l+1] else z[i]:=r-i+1;
      while (z[i]+i<=n+m+1)and(a[z[i]+1]=a[i+z[i]]) do inc(z[i]);
      if i+z[i]-1>r then
        begin
          l:=i;
          r:=i+z[i]-1;
        end;
    end;

  for i:=n+2 to n+m+1 do if z[i]=n then inc(k);
  writeln(k);
  k:=1000;
  for i:=n+2 to n+m+1 do if z[i]=n then
    begin
      if k>0 then write(i-n-2,' ');
      dec(k);
    end;
  writeln;
  close(output);
end.