社区讨论

求助各位大佬,用的py3,数据点下载到本地运行正确,交到网站上却全wa。。。

P3375【模板】KMP参与者 4已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@loc6z7tt
此快照首次捕获于
2023/10/30 08:58
2 年前
此快照最后确认于
2023/11/04 15:14
2 年前
查看原帖
求各位大佬看一下,谢谢大佬们!
CPP
s1=input()
s2=input()

def nextArr(son):
    next = [-1] * len(son)
    if len(son) > 1:
        next[1] = 0
        i, j = 0, -1
        while i < len(son) - 1:
            if j == -1 or son[i] == son[j]:
                i += 1
                j += 1
                next[i] = j
            else:
                j = next[j]
    return next

def kmp(mom,son):
    test=''
    if type(mom)!=type(test) or type(son)!=type(test):
        return -1
    if len(son)==0:
        return 0
    if len(mom)==0:
        return -1

    m=s=0
    next=nextArr(son)
    while m<len(mom):
        if s==-1 or mom[m]==son[s]:
            m+=1
            s+=1
        else:
            s=next[s]
        if s == len(son):
            print(m - s + 1)
            m=m-s+1
            s=0
    return -1
kmp(s1,s2)
border=nextArr(s2+' ')
s=str(border[1])+' '
for x in border[2::]:
    s+=str(x)+' '
print(s)

回复

3 条回复,欢迎继续交流。

正在加载回复...