Pagini recente » Cod sursa (job #2408746) | Cod sursa (job #1566001) | Cod sursa (job #1791467) | Cod sursa (job #1292855) | Cod sursa (job #422992)
Cod sursa(job #422992)
var p,t:array[0..1000001] of char;
i,j,l1,l2,n:longint;
k:array[0..1000001] of longint;
rez:array[0..1000] of longint;
te:text;
procedure kmp_index(var p:array of char;l:longint);
var i,j:longint;
begin
k[0]:=0; k[1]:=0;
for i:=2 to l do
begin
j:=k[i-1];
k[i]:=0;
repeat
if p[j+1]=p[i] then
begin
k[i]:=j+1;
break;
end
else j:=k[j];
until j=0;
end;
end;
procedure kmp_search(var p,t:array of char;l1,l2:longint);
var i,j:longint;
begin
kmp_index(p,l1);
i:=1; j:=0;
while (i<=l2) do
begin
if t[i]=p[j+1] then
begin
j:=j+1;
i:=i+1;
if j=l1 then
begin
n:=n+1;
if n<=1000 then rez[n]:=i-l1-1;
end;
end
else if j>0 then j:=k[j]
else i:=i+1;
end;
end;
begin
assign(te,'strmatch.in'); reset(te);
while(not(eoln(te))) do
begin
l1:=l1+1;
read(te,p[l1]);
end;
readln(te);
while(not(eoln(te))) do
begin
l2:=l2+1;
read(te,t[l2]);
end;
close(te);
assign(te,'strmatch.out'); rewrite(te);
kmp_search(p,t,l1,l2);
for i:=1 to l1 do writeln(i,'->',k[i]); readln;
writeln(te,n);
if n>1000 then n:=1000;
for i:=1 to n do write(te,rez[i],' ');
close(te);
end.