Skip to content

Commit bd9f001

Browse files
author
shengshijun
committed
update README
kmp算法:bugfix
1 parent abc6fb2 commit bd9f001

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
while origin_index < origin_len:
24-
if pattern[pattern_index] == origin[origin_index]:
25-
pattern_index += 1
26-
origin_index += 1
25+
# while需要放在前面,如果放在后面的话且有匹配的情况下pattern[pattern_index]就会越界
2726
while pattern_index > 0 and origin[origin_index] != pattern[pattern_index]:
2827
pattern_index = next_list[pattern_index]
28+
if pattern[pattern_index] == origin[origin_index]:
29+
pattern_index += 1
30+
origin_index += 1
31+
2932
if pattern_index == pattern_len:
3033
return origin_index - pattern_len
3134

3235

3336
def main():
34-
print match("sb", 'sb')
37+
print match("assssbsss", 'sb')
3538

3639

3740
if __name__ == "__main__":

0 commit comments

Comments
 (0)