Skip to content

Commit 66f6857

Browse files
committed
kmp算法
1 parent ca6b21e commit 66f6857

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,26 @@ life is short , use python
288288
➜ Py git:(master) ✗ python kmp.py
289289
Found 'sase' start at string 'asfdehhaassdsdasasedwa' 15 index position, find use times: 23
290290
Found 'sase' start at string '12s3sasexxx' 4 index position, find use times: 9
291+
292+
核心算法:
293+
def kmp(string, match):
294+
n = len(string)
295+
m = len(match)
296+
i = 0
297+
j = 0
298+
count_times_used = 0
299+
while i < n:
300+
count_times_used += 1
301+
if match[j] == string[i]:
302+
if j == m - 1:
303+
print "Found '%s' start at string '%s' %s index position, find use times: %s" % (match, string, i - m + 1, count_times_used,)
304+
return
305+
i += 1
306+
j += 1
307+
elif j > 0:
308+
j = j - 1
309+
else:
310+
i += 1
291311
```
292312

293313

0 commit comments

Comments
 (0)