Pagini recente » Cod sursa (job #188514) | Cod sursa (job #230843) | Monitorul de evaluare | Diferente pentru documentatie/rating intre reviziile 22 si 21 | Cod sursa (job #153977)
Cod sursa(job #153977)
program potrivire_siruri;
var a,b:array[1..2000001]of char;
s:ansistring;
pi:array[0..2000001]of Longint;
poz:array[1..1001]of Longint;
n,m,nr,i:Longint;
f:text;
procedure prefix;
var k:longint;
begin
k:=0;
pi[1]:=0;
for i:=2 to n 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;
end;
procedure kmp;
var q:longint;
begin
q:=0;
for i:=1 to m do
begin
while (q>0)and(a[q+1]<>b[i]) do q:=pi[q];
if a[q+1]=b[i] then q:=q+1;
if q=n then begin
inc(nr);
if nr<=1000 then poz[nr]:=i-n;
q:=pi[n];
end;
end;
end;
begin
assign(f,'strmatch.in');reset(f);
Readln(f,a);
Readln(f,b);
n:=length(a);
m:=length(b);
close(f);
prefix;
kmp;
assign(f,'strmatch.out');rewrite(f);
writeln(f,nr);
if nr>1000 then nr:=1000;
for i:=1 to nr do write(f,poz[i],' ');
close(f);
end.