Cod sursa(job #243331)

Utilizator cheery_g1rlHaller Emanuela cheery_g1rl Data 12 ianuarie 2009 18:27:58
Problema Potrivirea sirurilor Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 1.03 kb
var p,t:array[1..2000001] of char;
    c:char;
    n,m,l,q,k:longint;
    v,vv:array[1..2000001] of longint;
procedure prefix; inline;
    begin
      k:=0; v[1]:=0;
      for q:=2 to m do
        begin
          while (k>0)and(p[k+1]<>p[q]) do k:=v[k];
          if p[k+1]=p[q] then inc(k);
          v[q]:=k;
        end;
    end;
procedure kmp;inline;
    begin
      k:=0; l:=0;
for q:=1 to n do
   begin
     while (k>0)and(p[k+1]<>t[q]) do k:=v[k];
     if p[k+1]=t[q] then inc(k);
     if k=m then
        begin
        inc(l);
         if l<=1000 then vv[l]:=q-m;
          k:=v[k];
        end;
   end;

    end;
begin
assign(input,'strmatch.in'); reset(input);
m:=0;
while not seekeoln do begin inc(m); read(p[m]); end;
n:=0;
readln;
while not seekeoln do begin inc(n);read(t[n]); end;
close(input);

prefix;
kmp;

assign(output,'strmatch.out'); rewrite(output);
writeln(l);
if l>1000 then
   for k:=1 to 1000 do write(vv[k],' ')
     else
for k:=1 to l do write(vv[k],' ');
close(output);
end.