Pagini recente » Cod sursa (job #2558392) | Cod sursa (job #2963106) | Cod sursa (job #1299420) | Cod sursa (job #429310) | Cod sursa (job #747808)
Cod sursa(job #747808)
program kmp;
var f,g:text;
a,b:ansistring;
m,n,i,k,nr:longint;
x,v:array [1..200000] of longint;
bufin,bufout:array [1..65000] of byte;
procedure prefix;
var k,i:longint;
begin
k:=0; x[1]:=0;
for i:=2 to m do
begin
while (k>0) and (a[i]<>a[k+1]) do
k:=x[k];
if a[i]=a[k+1] then
k:=k+1;
x[i]:=k;
end;
end;
begin
assign (f,'strmatch.in'); reset (f);
assign (g,'strmatch.out'); rewrite (g);
settextbuf (F,bufin); settextbuf (g,bufout);
readln (f,a);
read (f,b);
m:=length(a); n:=length (b);
prefix;
k:=0;
for i:=1 to n do
begin
while (k>0) and (b[i]<>a[k+1]) do
k:=x[k];
if b[i]=a[k+1] then
k:=k+1;
if k=m then
begin
inc(nr); v[nr]:=i-k; k:=x[k];
end;
end;
writeln (g,nr);
if nr>1000 then
nr:=1000;
for i:=1 to nr do
write (g,v[i], ' ');
close (f); close (g);
end.