Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbd9f001

Browse files
author
shengshijun
committed
update README
kmp算法:bugfix
1 parentabc6fb2 commitbd9f001

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

‎string/kmp.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ def build_next():
2020

2121
origin_index,pattern_index=0,0
2222
next_list=build_next()
23+
print(next_list)
2324
whileorigin_index<origin_len:
24-
ifpattern[pattern_index]==origin[origin_index]:
25-
pattern_index+=1
26-
origin_index+=1
25+
# while需要放在前面,如果放在后面的话且有匹配的情况下pattern[pattern_index]就会越界
2726
whilepattern_index>0andorigin[origin_index]!=pattern[pattern_index]:
2827
pattern_index=next_list[pattern_index]
28+
ifpattern[pattern_index]==origin[origin_index]:
29+
pattern_index+=1
30+
origin_index+=1
31+
2932
ifpattern_index==pattern_len:
3033
returnorigin_index-pattern_len
3134

3235

3336
defmain():
34-
printmatch("sb",'sb')
37+
printmatch("assssbsss",'sb')
3538

3639

3740
if__name__=="__main__":

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp