nex数组:位置i的前缀子串的前缀与后缀最大相似值。
#include<bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
#define mp make_pair
#define pb emplace_back
#define mt(a,b) memset(a,b,sizeof(a))
#define zero(x) (((x)>0?(x):-(x))<eps)
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265;
const double eps = 0.000001;
const ll mod = 1e9 + 7;
const int maxn = 1e6 +7;
int nex[maxn];
char s[maxn], t[maxn];
int lt, ls;
int pos = 0;
void build_nex()
{
nex[0] = 0;
pos = 0;
for(int i = 2; i <= lt; i ++)
{
while(pos && t[pos + 1] != t[i])
pos = nex[pos];
if(t[pos + 1] == t[i])
pos ++;
nex[i] = pos;
}
}
void kmp()
{
pos = 0;
for(int i = 1; i <= ls; i ++)
{
while( pos && t[pos + 1] != s[i])
pos = nex[pos];
if(t[pos + 1] == s[i])
pos ++;
if(pos == lt)
{
printf("%d\n", i - lt + 1);
pos = nex[pos];
}
}
}
int main()
{
scanf("%s%s", s + 1, t + 1);
lt = strlen(t + 1);
ls = strlen(s + 1);
build_nex();
kmp();
for(int i = 1; i <= lt; i ++)
{
printf("%d ", nex[i]);
}
}
/*
3
1 2 2 1 2 2
*/