Pagini recente » Cod sursa (job #2445472) | Cod sursa (job #1490455) | Cod sursa (job #1352529) | Cod sursa (job #426861) | Cod sursa (job #302526)
Cod sursa(job #302526)
program kmp;
var a,b:array [1..2000000] of char;
nrsol,i,k,m,n:longint;
pi:array [1..2000000] of longint;
sol:array [1..1000] of longint;
begin
assign(input,'strmatch.in');
assign(output,'strmatch.out');
reset(input);
rewrite(output);
m:=0;
while not eoln() do
begin
inc(m);
read(a[m]);
end;
readln;
n:=0;
while not eoln() do
begin
inc(n);
read(b[n]);
end;
k:=0;
pi[1]:=0;
for i:=2 to m do
begin
while (k>0) and (a[k+1]<>a[i]) do
k:=pi[k];
if a[k+1] = a[i] then
inc(k);
pi[i]:=k;
end;
k:=0;
for i:=1 to n do
begin
while (k>0) and (a[k+1]<>b[i]) do
k:=pi[k];
if a[k+1] = b[i] then
inc(k);
if k = m then
begin
inc(nrsol);
if nrsol<=1000 then
sol[nrsol]:=i-m;
k:=pi[k];
end;
end;
writeln(nrsol);
if nrsol>1000 then
nrsol:=1000;
for i:=1 to nrsol do
write(sol[i],' ');
close(input);
close(output);
end.