码迷,mamicode.com
首页 > 编程语言 > 详细

KMP算法

时间:2015-04-01 21:52:37      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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.

KMP算法

标签:

原文地址:http://blog.csdn.net/bugall/article/details/44813855

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!