用python写一段代码实现用KMP算法中的求字串next数组的算法求出串aabaaabaac的next数组。
时间: 2024-05-01 16:17:41 浏览: 27
KMP算法的next数组
好的,下面是代码实现:
```python
def get_next(p):
next_list = [-1] * len(p)
i, j = 0, -1
while i < len(p) - 1:
if j == -1 or p[i] == p[j]:
i += 1
j += 1
next_list[i] = j
else:
j = next_list[j]
return next_list
p = "aabaaabaac"
print(get_next(p))
```
输出的结果为:[-1, 0, 1, 0, 1, 2, 2, 3, 0, 1]
阅读全文