标签:
program KMP(input, output);
var
fail:array[1..1000] of longint ;
s1,s2:string;
{==============================================}
procedure find;
var
i,j:longint;
begin
fail[1]:=0;
j:=fail[1];
for i:=2 to length(s2) do
begin
while (j > 0) and (s2[j + 1] <> s2[i]) do j:=fail[j];
if s2[j + 1] = s2[i] then inc(j);
fail[i]:=j;
end;
end;
{==============================================}
procedure solve;
var
i,j:longint;
begin
j:=0;
for i:=1 to length(s1) do
begin
while (j > 0) and (s2[j + 1] <> s1[i]) do j:=fail[j];
if s2[j + 1] = s1[i] then inc(j);
if j = length(s2) then
begin
writeln(i - j + 1);
j:=fail[j];
end;
end;
end;
{==============================================}
procedure init;
begin
readln(s1);
readln(s2);
end;
{==============================================}
begin
init;
find;
solve;
end.
标签:
原文地址:http://blog.csdn.net/bugall/article/details/44813855