Cod sursa(job #1599546)

Utilizator VandheerManPopescu Alin VandheerMan Data 13 februarie 2016 23:08:27
Problema Potrivirea sirurilor Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 1.17 kb
program potisir;

type
 arch=array[1..2000005] of char;
 sir1=array[1..2000005] of longint;
var
 a,b:arch;
 f:text;
 q,i,n,m,m1:longint;
 pi,pos:sir1;
begin
m:=0;
m1:=0;
assign(f,'strmatch.in');
reset(f);
while (not(eoln(f))) do
        begin
         m:=m+1;
         read(f,a[m]);
        end;
readln(f);
while (not(eof(f)))
 do begin
  inc(m1);
  read(f,b[m1]);
  end;
q:=0;
n:=0;
pi[1]:=0;
for i:=2 to m do
        begin
         while ((q>0) and (a[q+1]<>a[i])) do
                begin
                 q:=pi[q];
                end;
         if (a[q+1]=a[i]) then inc(q);
         pi[i]:=q;
        end;
q:=0;
for i:=1 to m1 do
        begin
         while ((q>0) and (a[q+1]<>b[i]))
                do
                 begin
                  q:=pi[q];
                 end;
         if ( a[q+1]=b[i]) then q:=q+1;
         if (q=m) then
                begin
                 q:=pi[m];
                 n:=n+1;
                 if (n<=1000) then pos[n]:=i-m;
                end;
        end;

close(f);
assign(f,'strmatch.out');
rewrite(f);
writeln(f,n);
if n>1000 then n:=1000;
for i:=1 to n do write(f,pos[i],' ');
close(f);
end.