KMP算法模板
自己对KMP算法不是很熟,特意写了一篇KMP模拟。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FR freopen("in.txt", "r", stdin)
#define FW freopen("out.txt", "w", stdout)
struct KMP
{
int next[1000005];
string pattern;
void init(const string &a)
{
next[0] = 0;
for (int i = 1; i < a.size(); i++)
{
int p = next[i - 1];
while (p != 0 && a[i] != a[p])
{
p = next[p - 1];
}
next[i] = a[i] == a[p] ? p + 1 : 0;
}
pattern = a;
}
void printNext() const
{
for (int i = 0; i < pattern.size(); i++)
printf("%d ", next[i]);
}
void match(const string &text) const
{
int i = 0;
int j = 0;
while (j < text.size())
{
if (pattern[i] == text[j])
{
i++;
j++;
if (i == pattern.size())
{
// OK
printf("%d\n", j - i + 1);
i = next[i - 1];
}
}
else
{
if (i == 0)
j++;
else
i = next[i - 1];
}
}
}
} kmp;
int main()
{
string s1, s2;
cin >> s1 >> s2;
kmp.init(s2);
kmp.match(s1);
kmp.printNext();
return 0;
}
例题
结论 n − n e x t [ n ] n - next[n] n−next[n]。
#include<cstdio>
using namespace std;
const int maxn=1111111;
int n,kmp[maxn];//kmp数组即next数组
char ss[maxn];
int main()
{
scanf("%d%s",&n,ss+1);
int j=0;
for(int i=2;i<=n;++i)
{
while(j&&ss[i]!=ss[j+1]) j=kmp[j];
if(ss[i]==ss[j+1]) ++j;
kmp[i]=j;
}
printf("%d",n-kmp[n]);
return 0;
}
#include <bits/stdc++.h>
#define FR freopen("in.txt", "r", stdin)
using namespace std;
typedef unsigned long long ll;
int nxt[1000005];
int mit[1000005];
ll ans = 0;
void kmp(string &str)
{
for (int i = 1; i < str.size(); i++)
{
int curr = nxt[i - 1];
while (curr > 0 && str[curr] != str[i])
curr = nxt[curr - 1];
nxt[i] = str[curr] == str[i] ? curr + 1 : 0;
}
for (int i = 1; i < str.size(); i++)
{
int j = nxt[i];
for (; j != 0 && nxt[j - 1] != 0; j = nxt[j - 1])
;
nxt[i] = j;
if (j != 0)
ans += i - j + 1;
}
}
int main()
{
int n;
cin >> n;
string str;
cin >> str;
kmp(str);
cout << ans;
return 0;
}
#include <bits/stdc++.h>
#define FR freopen("in.txt", "r", stdin)
using namespace std;
typedef unsigned long long ll;
int nxt[50];
int sta[50];
int match[50];
int top = 0;
void kmp(string &str)
{
for (int i = 1; i < str.size(); i++)
{
int curr = nxt[i - 1];
while (curr > 0 && str[curr] != str[i])
curr = nxt[curr - 1];
nxt[i] = str[i] == str[curr] ? curr + 1 : 0;
}
}
int main()
{
string txt, pat;
cin >> txt >> pat;
kmp(pat);
int j = 0;
for (int i = 0; i < txt.size(); i++)
{
while (j > 0 && txt[i] != pat[j])
j = nxt[j - 1];
if (txt[i] == pat[j])
j++;
match[i] = j;
sta[top++] = i;
if (j == pat.size())
{
top -= j;
j = match[sta[top - 1]];
}
}
for (int i = 0; i < top; i++)
{
cout << txt[sta[i]];
}
return 0;
}
#include <bits/stdc++.h>
#define FR freopen("in.txt", "r", stdin)
using namespace std;
typedef unsigned long long ll;
ll nxt[1000005];
ll nxt2[1000005];
ll cnt[1000005];
ll ans[1000005];
ll mod = 1000000007;
void kmp(string &str)
{
for (int i = 1; i < str.size(); i++)
{
int curr = nxt[i - 1];
while (curr > 0 && str[curr] != str[i])
curr = nxt[curr - 1];
nxt[i] = str[i] == str[curr] ? curr + 1 : 0;
}
for (int i = 1; i < str.size(); i++)
{
cnt[i] = nxt[i] != 0 ? cnt[nxt[i] - 1] + 1: 0;
}
for (int i = 1; i < str.size(); i++)
{
int curr = nxt2[i - 1];
while (curr > 0 && (str[curr] != str[i] || (curr + 1) * 2 > i + 1))
curr = nxt[curr - 1];
nxt2[i] = str[i] == str[curr] ? curr + 1 : 0;
}
for (int i = 1; i < str.size(); i++)
{
ans[i] = nxt[i] != 0 ? cnt[nxt2[i] - 1] + 1 : 0;
}
}
void solve()
{
string str;
cin >> str;
kmp(str);
ll Ans = 1;
for (int i = 1; i < str.size(); i++)
{
Ans = (Ans * (ans[i] + 1)) % mod;
}
cout << Ans << endl;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
solve();
}
return 0;
}