(T: str, p: str)
| 1 | # sunday 算法,T 为文本串,p 为模式串 |
| 2 | def 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] 表示遇到坏字符可以向右移动的距离 |
no test coverage detected