我是靠谱客的博主 斯文小鸽子,这篇文章主要介绍KMP模板,现在分享给大家,希望可以做个参考。

题目如下 :

题解代码如下:

n = int(input())
p = input()
m = int(input())
s = input()

def get_next(p):
    l = len(p)
    next_ = [-1]
    i = 0
    j = -1
    while i < l:
        if j == -1 or p[i] == p[j]:
            i += 1
            j += 1
            next_.append(j)
        else:
            j = next_[j]
    return next_

next_ = get_next(p)

i = 0
j = 0
while i < m:
    if j == -1 or p[j] == s[i]:
        i += 1
        j += 1
    else:
        j = next_[j]
    if j == n:
        print(i - n,end = " ")
        j = next_[j]

最后

以上就是斯文小鸽子最近收集整理的关于KMP模板的全部内容,更多相关KMP模板内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部