Cod sursa(job #537131)

Utilizator FLORINSTELISTUOprea Valeriu-Florin FLORINSTELISTU Data 20 februarie 2011 11:04:49
Problema Potrivirea sirurilor Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.3 kb
program STRMATCH;
type vect=array[0..2000002]of char;
var  f,g:text;n,m,i,k,x,nr:int64;ch:char;
     p,t:vect;
     l,poz:array[0..2000002]of int64;
procedure urmatorul(var p:vect);
var k,i:int64;
begin
     k:=-1;
      l[0]:=0;
       for i:=1 to m-1 do begin
        while (k>0)and(p[k+1]<>p[i]) do k:=l[k];
         if p[k+1]=p[i] then inc(k);
          l[i]:=k;
           end;
         end;

begin
    assign(f,'strmatch.in');reset(f);
    assign(g,'strmatch.out');rewrite(g);
       k:=0;
          while not eoln(f) do begin
           read(f,ch);
           p[m]:=ch;
           inc(m);
            end;readln(f);
          while not eoln(f) do begin
           read(f,ch);
           t[n]:=ch;
           inc(n);
            end;
             urmatorul(p);
              for i:=0 to n-1 do begin
                while (x>0)and(p[x+1]<>t[i]) do x:=l[x];
                  if p[x+1]=t[i] then inc(x);
                    if (x=m-1) then begin
                    if nr>1000 then break;
                      inc(nr);
                      poz[nr]:=i-m+1;
                     x:=l[x];
                           end;
                         end;
                  writeln(g,nr);
                   for i:=1 to nr do write(g,poz[i],' ');
            CLOSE(F);close(g);
          end.