MCPcopy
hub / github.com/itcharge/AlgoNote / sunday

Function sunday

codes/python/04_string/string_sunday.py:2–15  ·  view source on GitHub ↗
(T: str, p: str)

Source from the content-addressed store, hash-verified

1# sunday 算法,T 为文本串,p 为模式串
2def sunday(T: str, p: str) -> int:
3 n, m = len(T), len(p)
4
5 bc_table = generateBadCharTable(p) # 生成后移位数表
6
7 i = 0
8 while i <= n - m:
9 j = 0
10 if T[i: i + m] == p:
11 return i # 匹配完成,返回模式串 p 在文本串 T 的位置
12 if i + m >= n:
13 return -1
14 i += bc_table.get(T[i + m], m + 1) # 通过后移位数表,向右进行进行快速移动
15 return -1 # 匹配失败
16
17# 生成后移位数表
18# bc_table[bad_char] 表示遇到坏字符可以向右移动的距离

Callers 1

string_sunday.pyFile · 0.85

Calls 1

generateBadCharTableFunction · 0.70

Tested by

no test coverage detected