Skip to content

Commit 85fce78

Browse files
author
shengshijun
committed
字符串朴素匹配算法
1 parent ba08647 commit 85fce78

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

string/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__ = 'shenshijun'

string/simple.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__ = 'shenshijun'
4+
5+
6+
def match(origin, pattern):
7+
origin_index, pattern_index = 0, 0
8+
pattern_len = len(pattern)
9+
while origin_index < len(origin):
10+
for pattern_index in xrange(pattern_len):
11+
if pattern[pattern_index] != origin[origin_index]:
12+
origin_index -= (pattern_index - 1)
13+
break
14+
else:
15+
origin_index += 1
16+
pattern_index += 1
17+
18+
if origin[origin_index] == pattern[pattern_index]:
19+
return origin_index - pattern_index
20+
21+
22+
def main():
23+
print match("absbsbshdhhd", 'sb')
24+
25+
26+
if __name__ == "__main__":
27+
main()

0 commit comments

Comments
 (0)