Cod sursa(job #1219808)

Utilizator Alex1199Alex Bercea Alex1199 Data 15 august 2014 11:54:34
Problema Potrivirea sirurilor Scor 24
Compilator fpc Status done
Runda Arhiva educationala Marime 1 kb
var
  s, p : ansistring;
  l : array[1..2000000] of longint;
  k, i, u: longint;
  q : array[1..1000]  of longint;
begin
 assign(input,'strmatch.in');
  reset(input);
 assign(output,'strmatch.out');
  rewrite(output);
 readln(p);
 readln(s);

 l[1]:=0;
 for i:=2 to length(p) do
    begin
     k:=l[i-1];
       while (k>0) and (p[k+1]<>p[i]) do
       begin
        u:=u+1;
         k:=l[k];
       end;
       if p[k+1]=p[i] then
         inc(k);
       l[i]:=k;
    end;

 k:=0;
  for i:=1 to length(s) do
   begin
     while (k>0) and (p[k+1]<>s[i]) do
      k:=l[k];
     if p[k+1]=s[i] then
       inc(k);
     if k=length(p) then
     begin
       inc(u);
       if u<1000 then
                   q[u]:=i-length(p);
       k:=l[k];
     end;
   end;
   writeln(u);
   if u>1000 then
              for i:=1 to 1000 do
                    write(q[i],' ')
                   else
               for i:=1 to u do
                    write(q[i],' ');
  Close(output);
end.