def strrindex(s, t): if t not in s: return -1 i = len(s) - 1 while i >= 0: if s[i:i+len(t)] == t: return i i -= 1 return -1函数如何输出
时间: 2023-12-11 08:05:19 浏览: 33
这个函数的作用是在字符串s中查找子串t最后一次出现的位置。如果t不在s中出现,则返回-1。如果t在s中出现,则返回最后一次出现的位置。
例如,如果s是"hello world",t是"o",则函数会返回7,因为"o"最后一次出现在"s"的第7个位置。如果t是"z",则函数会返回-1,因为"z"不在"s"中出现。
你可以这样调用这个函数,比如:
```
s = "hello world"
t = "o"
print(strrindex(s, t)) # 输出 7
t = "z"
print(strrindex(s, t)) # 输出 -1
```
相关问题
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
```
def strrindex(s,t): pos = 0 pos1 = -1 while True: pos = s.find(t,pos) if pos == -1: else: pos1 = pos pos = pos + len(t) if __name__ == "__main__": s=input() t=input() print(strrindex(s,t))
这段代码中缺少了一些关键的语句和逻辑,无法正常运行。这里提供一份完整的strrindex函数的代码实现,您可以参考一下:
```python
def strrindex(s, t):
n, m = len(s), len(t)
if m > n:
return -1
pos1 = -1
while True:
pos = s.find(t, pos1 + 1)
if pos == -1:
break
pos1 = pos
return pos1
if __name__ == "__main__":
s = input().strip()
t = input().strip()
print(strrindex(s, t))
```
使用该函数可以很方便地找到t在s中最右边出现的位置。例如,如果输入s="The strdup() function new returns a pointer to a new string",t="new",则输出31,即"new"在s中最右边出现的位置。如果输入s="hello world"和t="x",则输出-1,表示s中不含有t。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)