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

Commitabc6fb2

Browse files
author
shengshijun
committed
update README
kmp算法
1 parent0cd9ba1 commitabc6fb2

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

‎README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ algorithm
4545
##字符串匹配算法
4646
1. 朴素算法
4747
2. Rabin-Karp算法
48-
3. 有限自动机字符串匹配
49-
4. KMP算法
48+
3. KMP算法
5049

5150

5251
#数据结构

‎string/kmp.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__='shenshijun'
4+
5+
6+
defmatch(origin,pattern):
7+
origin_len=len(origin)
8+
pattern_len=len(pattern)
9+
10+
defbuild_next():
11+
_next_list= [0forxinxrange(pattern_len)]
12+
last_match=0
13+
forcur_indexinxrange(1,pattern_len):
14+
whilelast_match>0andpattern[last_match]!=pattern[cur_index]:
15+
last_match=_next_list[last_match]
16+
ifpattern[last_match]==pattern[cur_index]:
17+
last_match+=1
18+
_next_list[cur_index]=last_match
19+
return_next_list
20+
21+
origin_index,pattern_index=0,0
22+
next_list=build_next()
23+
whileorigin_index<origin_len:
24+
ifpattern[pattern_index]==origin[origin_index]:
25+
pattern_index+=1
26+
origin_index+=1
27+
whilepattern_index>0andorigin[origin_index]!=pattern[pattern_index]:
28+
pattern_index=next_list[pattern_index]
29+
ifpattern_index==pattern_len:
30+
returnorigin_index-pattern_len
31+
32+
33+
defmain():
34+
printmatch("sb",'sb')
35+
36+
37+
if__name__=="__main__":
38+
main()
39+
40+
41+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp