next[0] = -1 i, j = 0, -1 while i < len(t) - 1: if j == -1 or t[i] == t[j]: i, j = i + 1, j + 1 next[i] = j else: j = next def StrCount2(s, t): # KMP算法求解 i, j, cnt = 0, 0, 0 next = [-1] * len(t) GetNext(t, next) while i < len(s): if j == -1 or s[i] == t[j]: i, j = i + 1, j + 1 else: j = next[j] if j == len(t): cnt += 1 j = 0 return cnt
时间: 2024-02-01 22:02:32 浏览: 27
这段代码是用 Python 实现的 KMP 算法,可以用于在字符串 s 中查找模式串 t 出现的次数。其中 GetNext 函数用于计算 t 的 next 数组,StrCount2 函数是主函数,用于实现 KMP 算法。
具体实现步骤如下:
1. 声明变量 i, j, cnt,用于记录当前扫描到的位置、模式串匹配的位置和出现次数。
2. 初始化 next 数组为 -1,调用 GetNext 函数计算 t 的 next 数组。
3. 在 s 中循环扫描,如果当前字符匹配,则 i, j 均加 1,否则 j 跳转到 next[j] 的位置。
4. 如果 j 等于 len(t),说明找到了一次匹配,cnt 加 1,j 重置为 0。
5. 最终返回 cnt,即模式串 t 在 s 中出现的次数。
相关问题
def GetNext(t,next): #由模式串t求出next值 next[0] = -1 i,j=0,-1 while i < len(t) - 1: if j == -1 or t[i] == t[j]: i,j=i+1, j+1 next[i] = j else: j=next def StrCount2(s, t): # KMP算法求解 i, j, cnt = 0, 0, 0 next = [-1] * len(t) GetNext(t, next) while i < len(s): if j == -1 or s[i] == t[j]: i, j = i + 1, j + 1 else: j = next[j] if j == len(t): cnt += 1 j = 0 return cnt
这段代码存在一些问题,我将逐一列举并给出修改建议:
1. GetNext 函数中的第 7 行和第 9 行缺少缩进,导致语法错误。正确的代码应该是:
```python
def GetNext(t, next):
next[0] = -1
i, j = 0, -1
while i < len(t) - 1:
if j == -1 or t[i] == t[j]:
i, j = i + 1, j + 1
next[i] = j
else:
j = next[j]
```
2. GetNext 函数中的第 8 行和第 10 行缺少 j 的更新,导致 next 数组计算错误。正确的代码应该是:
```python
def GetNext(t, next):
next[0] = -1
i, j = 0, -1
while i < len(t) - 1:
if j == -1 or t[i] == t[j]:
i, j = i + 1, j + 1
next[i] = j if t[i] != t[j] else next[j]
else:
j = next[j]
```
3. StrCount2 函数中的第 10 行缺少缩进,导致语法错误。正确的代码应该是:
```python
def StrCount2(s, t):
# KMP算法求解
i, j, cnt = 0, 0, 0
next = [-1] * len(t)
GetNext(t, next)
while i < len(s):
if j == -1 or s[i] == t[j]:
i, j = i + 1, j + 1
else:
j = next[j]
if j == len(t):
cnt += 1
j = 0
return cnt
```
综上所述,修改后的代码如下:
```python
def GetNext(t, next):
next[0] = -1
i, j = 0, -1
while i < len(t) - 1:
if j == -1 or t[i] == t[j]:
i, j = i + 1, j + 1
next[i] = j if t[i] != t[j] else next[j]
else:
j = next[j]
def StrCount2(s, t):
# KMP算法求解
i, j, cnt = 0, 0, 0
next = [-1] * len(t)
GetNext(t, next)
while i < len(s):
if j == -1 or s[i] == t[j]:
i, j = i + 1, j + 1
else:
j = next[j]
if j == len(t):
cnt += 1
j = 0
return cnt
```
MaxSize=100 #基于BF算法 def StrCount1(s,t): #BF算法求解 i,cnt=0,0 while i<len(s)-len(t)+1: j,k=i,0 while j<len(s) and k<len(t) and s[j]==t[k]: j,k=j+1,k+1 if k==len(t): #找到一个子串 cnt+=1 #累加出现的次数 i=j #i从j开始 else: i+=1 #i增加1 return cnt #基于KMP算法 def GetNext(t,next): #由模式串t求出next值
def GetNext(t, next):
next[0] = -1
i, j = 0, -1
while i < len(t) - 1:
if j == -1 or t[i] == t[j]:
i, j = i + 1, j + 1
next[i] = j
else:
j = next[j]
def StrCount2(s, t):
# KMP算法求解
i, j, cnt = 0, 0, 0
next = [-1] * len(t)
GetNext(t, next)
while i < len(s):
if j == -1 or s[i] == t[j]:
i, j = i + 1, j + 1
else:
j = next[j]
if j == len(t):
cnt += 1
j = 0
return cnt
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)