#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
#define LL long long
using ull = unsigned long long;
const LL N = 1e6 + 5, mod = 1e9 + 7;
char p[N], s[N];//p为子串,s为主串
int ne[N],n,m;//ne是next数组,存最大相同前后缀
int main() {
cin >> m;
cin >> p + 1;
cin >> n;
cin >> s + 1;
for (int i = 2, j = 0; i <= m; i++)//求next数组
{
while (j&&p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1])
j++;
ne[i] = j;
}
for (int i = 1, j = 0; i <=n; i++)//和主串匹配
{
while (j&&s[i] != p[j + 1])j = ne[j];
if (s[i] == p[j + 1])
j++;
if (j == m)
{
printf("%d ", i - j);
}
}
return 0;
}文章来源地址https://www.toymoban.com/news/detail-725821.html
文章来源:https://www.toymoban.com/news/detail-725821.html
到了这里,关于kmp算法模板(acwing831)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!