前文介绍了字符串的基本操作,本节来介绍字符串中的经典问题——匹配。
利用滑动窗口进行逐项对比,但是问题在于每次对比失败都只进一位,重头开始对比,这样效率较低。
Python实现:
def bf_matching(s1, s2):
a, b, c = 0, 0, 0
res = []
while a + len(s2) - 1 < len(s1):
if s1[b] == s2[c]:
b += 1
c += 1
if c == len(s2):
res.append(a)
a += 1
c = 0
b = a
else:
a += 1
b = a
c = 0
return res
构造子串的前缀表,可以避免大量的重复对比,如果遇到字符串不相等的情况时,可以根据前缀表中的信息免去多余的对比。
Python实现:
def get_next(s):
j = 0
n = len(s)
pnext = [0, 0]
for i in range(1, n):
while j > 0 and s[j] != s[i]:
j = pnext[j]
if s[j] == s[i]:
j += 1
pnext.append(j)
return pnext
def kmp_matching(s1, s2):
n, m = len(s1), len(s2)
pnext = get_next(s2)
j = 0
for i in range(1, n):
while j > 0 and s1[i] != s2[j]:
j = pnext[j]
if s1[i] == s2[j]:
j += 1
if j == len(s2):
return i - m + 1
return -1
if __name__ == ‘__main__‘:
s1 = ‘abcdefgbcdehhhhbcde‘
s2 = ‘abcabcbac‘
print(bf_matching(s1, s2))
print(get_next(s2))
print(kmp_matching(s1, s2))
原文:https://www.cnblogs.com/yiwen-statistics/p/13817614.html