KMP中的一个重要思想与AC自动机非常相似,就是失配指针。在kmp算法中指匹配失败后该返回到字符串的哪个位置,也就是俗称的特定位置的复活出生点。
KMP模板如下:
//#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include<queue>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
#define debug cout<<"procedures above are available"<<endl;
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 33;
inline ll nextlong()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
/**Maintain your determination.Nobody knows the magnificent landscape
at his destination before the arrival with stumble.**/
/**Last Remote**/
string text,sub;
ll nexxt[500000];
void getnext()
{
ll pt = 0, pt2 = -1;
nexxt[0] = -1;//初始出生点为边缘外
while (pt < sub.size())
{
if (pt2 == -1 || sub[pt2] == sub[pt])
{
nexxt[++pt] = ++pt2;//如果某个字母在字符串中重复出现,那么确定它的复活出生点
}
else
pt2 = nexxt[pt2];//否则判定死亡,返回上一个出生点
}
}
ll cnt = 0;
void kmp()
{
getnext();
ll pt1 = 0, pt2 = 0;
while (pt1 < text.size())
{
//cout << pt1 << " " << pt2 << end