Cod sursa(job #872180)

Utilizator Buzu_Tudor_RoCont vechi Buzu_Tudor_Ro Data 5 februarie 2013 21:07:00
Problema Potrivirea sirurilor Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.31 kb
Program strmatch_arena_edu;
var fi,fo : text; i,m,n,k,nrsol : longint;
    a,b : array[1..2000001] of char;   r:set of char;
    sol:array[0..1010] of longint; pi:array[0..2000000] of longint;
    bufi,bufo:array[0..1 shl 20] of char;
begin
   assign(fi,'strmatch.in'); reset(fi);
   assign(fo,'strmatch.out'); rewrite(fo);
   settextbuf(fi,bufi); settextbuf(fo,bufo);
   pi[1]:=0; k:=0; m:=1; n:=1;   r:=['A'..'Z']+['a'..'z']+['0'..'9'];
   readln(fi,a); read(fi); readln(fi,b);
   while a[m] in r do m:=m+1;
   while b[n] in r do n:=n+1;
   m:=m-1; n:=n-1;
   for i:=2 to m do begin
                    while (k>0) and (a[k+1]<>a[i]) do k:=pi[k];
                    if a[k+1]=a[i] then k:=k+1;
                    pi[i]:=k;
                    end;

   k:=0; nrsol:=0;
   for i:=1 to n do begin
                    while (k>0) and (a[k+1]<>b[i]) do k:=pi[k];
                    if a[k+1]=b[i] then k:=k+1;
                    if k=m then begin
                                k:=pi[m];
                                nrsol:=nrsol+1;
                                if nrsol<=1000 then sol[nrsol]:=i-m;
                                end;
                    end;

   writeln(fo,nrsol);
   if nrsol>1000 then nrsol:=1000;
   for i:=1 to nrsol do write(fo,sol[i],' ');
   close(fi); close(fo);
end.