Mai intai trebuie sa te autentifici.

Cod sursa(job #1599498)

Utilizator VandheerManPopescu Alin VandheerMan Data 13 februarie 2016 22:02:08
Problema Potrivirea sirurilor Scor 14
Compilator fpc Status done
Runda Arhiva educationala Marime 1.01 kb
program potisir;

type
 sir=string;
 sir1=array[1..2000005] of longint;
var
 a,b:sir;
 f:text;
 q,i,n,m:longint;
 pi,pos:sir1;
begin
assign(f,'strmatch.in');
reset(f);
readln(f,a);
read(f,b);
q:=0;
n:=0;
m:=length(a);
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 length(b) 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.