def compute_fail(pattern): fail = [0 for i in range(len(pattern))] m = len(pattern) j = 0 i = 1 while i < m: if pattern[i] == pattern[j]: fail[i] = j + 1 j += 1 i += 1 elif j > 0: j = fail[j-1] #這里為什么要回到上個(gè)字節(jié)匹配的值,而不是直接從0開(kāi)始。 else: fail[i] = 0 i += 1 return fail
在下個(gè)字節(jié)匹配失敗之后,為什么不直接從開(kāi)始匹配,而是要回到上個(gè)字節(jié)匹配的位置?