问题描述:
完整代码:
k=int(input()) #输入k值
s = input() #输入字符串
all=0 #统计相关性数量
str1,str2='Alice','Bob'
def f(str,q): #定义一个函数,用于获取Alice和Bob在句段中所有的位置
ls=[] #将他们所处的位置放回到此列表ls中
index = 0
begin = 0
while (index != -1) and (begin <= len(s)): # 设置能够继续搜素的条件
index = s.find(str, begin)
begin = index + 1
if index == 0: # 当str处于句首时
if not s[index + q].isalpha(): # 用于判断str后面是否还有字母。index+q是因为str的字符串长度为q个长度
ls.append(index)
elif index + q == len(s): # 整段话最后一个字符串是str的情况
if not s[index - 1].isalpha(): # 判断此‘Alice’前面是否还有字符串
ls.append(index)
else: #str处于句段中间的情况
if (not s[index + q].isalpha()) and (not s[index - 1].isalpha()): #‘Alice’前后都是标点
ls.append(index)
return A
ls1=f(str1,5) #Bob和Alice的长度不一样,一个是3,另一个是5.使用函数时就需要传参q
ls2=f(str2,3)
for i in range(len(ls2)):
for j in range(len(ls1)):
if ls2[i]>ls1[j] and ls2[i]-ls1[j]-5<=k:
all+=1
else:
continue
for i in range(len(ls1)):
for j in range(len(ls2)):
if ls2[j]<ls1[i] and ls1[i]-ls2[j]-3<=k:
all+=1
print(all)