kmp 为一个图 而深入认识 ,用输出来说话

来源:互联网 发布:js event 编辑:程序博客网 时间:2024/05/27 00:45

http://www.cnblogs.com/titer1/archive/2012/04/01/2429413.html 

图 来自v_july_v 博客图

 

2012年4月1日

21:46

clip_image002

 

pattern_index: 0

debug luky target_index: 1

对应 july图中 index =0

pattern_index: 1

debug luky target_index: 2

pattern_index: 2

debug luky target_index: 3

pattern_index: 3

when   overlay ,debug target_index: 3

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 4

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 5

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 6

pattern_index: 0

debug luky target_index: 7

对应 july图中 index =6

pattern_index: 1

debug luky target_index: 8

pattern_index: 2

when   overlay ,debug target_index: 8

pattern_index: 0

debug luky target_index: 9

对应 july图中 index =8,

图中没有标识

pattern_index: 1

when   overlay ,debug target_index: 9

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 10

pattern_index: 0

debug luky target_index: 11

对应 july图中 index =10

pattern_index: 1

when   overlay ,debug target_index: 11

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 12

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 13

pattern_index: 0

debug luky target_index: 14

对应 july图中 index =13

pattern_index: 1

debug luky target_index: 15

pattern_index: 2

debug luky target_index: 16

pattern_index: 3

debug luky target_index: 17

pattern_index: 4

when   overlay ,debug target_index: 17

pattern_index: 1

这里 发现问题 不一致的bug 作者给出序

  程序中的

string src="annbcdanacadsannannabnna";

string pattern=                         "annacanna"

;

实际是:

string src="annbcdanacadsanannacanna"

;就是故意让最后的结尾满足了pattern..

debug luky target_index: 18

pattern_index: 2

debug luky target_index: 19

pattern_index: 3

debug luky target_index: 20

 

pattern_index: 4

when   overlay ,debug target_index: 20

pattern_index: 1

when   overlay ,debug target_index: 20

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 21

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 22

pattern_index: 0

when   pattern_index ==0 ,debug target_index: 23

pattern_index: 0

debug luky target_index: 24

-1

 

 

 

 

作者的勘误:

 

另一作者saturnman发现,在上述KMP匹配过程图中,index=8和index=11处画错了。还有,anaven也早已发现,index=3处也画错了。非常感谢。但图已无法修改,见谅。

我和 zy的讨论

clip_image001

zy

index = 0 时 annb 算到 k = -1; 所以应该向后移4-(-1+1)次

66

简言之,index=3应该 pass,

 

为什么这个在我的输出力没有检测 出来??

zy

index = 6 时  ana 算到 k = 0 所以应该向后移 3-0+1)次

66

就是说 index=8不应该被 pass

第8 11 处的错找不到。。