字符串匹配算法
def brute_force(t: str, p: str): # 暴力破解 i = 0 while i < len(t): j = 0 while j < len(p): if t[i + j] != p[j]: break j = j + 1 if j == len(p): return i i = i + 1 return Falsedef brute_force1(s: str, p: str): # aaaab, aaab # 暴力破解, 很接近kmp+3 i = 0 j = 0 temp = 0 while i < len(s) and j < len(p): # 考虑去掉一重f循环, 当内外层循环控制变量会有某种联系时候 if (s[i] == p[j]): j = j + 1 temp = temp + 1 else: # 当前匹配失败(j的更新没有考虑前后缀问题) j = 0 # 恢复j i = i - temp # 恢复i temp = 0 i = i + 1 if j == len(p): return i - len(p) else: return Falsedef get_next(p: str) -> []: # 基于前缀后缀 # next: list[int] = [0] count = 0 i = 0 j = 0 s = p[1:] p = p[:len(p) - 1] while i < len(s): # i 控制的是 当前 串的后缀, j 是前缀。只考虑当前串, 后缀某个字符开始, 和字符串开头字符开始匹配, 取其最大, 称为最大公共字串 if (s[i] == p[j]): j = j + 1 next.append(j) else: # if j == 0: # s_i ! = p_j and j == 0 表示第一个就匹配不上, 必定没有公共缀 next.append(j) else: # s_i != p_J and j != 0 可能从头还会匹配上, 保持i j = 0 continue i = i + 1 return nextdef kmp(s: str, p: str) -> bool: i = 0 j = 0 next = get_next(p) while i < len(s) and j < len(p): if (s[i] == p[j]): j = j + 1 else: # j = next[j - 1] + 1 i = i + 1 if j == len(p): return i - len(p) else: return Falseif __name__ == '__main__': print(brute_force('helloworld', 'world')) print(brute_force1('helloworld', 'world')) print(kmp('helloworld', 'world'))s = 'aaaab'p = 'aaab'print(kmp(s, p))
总结