A P3375 【模板】KMP字符串匹配
#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
char b[1000005], a[1000005]; int n, m, x, nex[1000005];
int main()
{
while (cin >> b) {
if (b[0] == '#') break;
cin >> a;
m = strlen(b);
n = strlen(a);
nex[1] = 0;
int N = 0;
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && a[i - 1] != a[j]) j = nex[j];
if (a[i - 1] == a[j]) j++;
nex[i] = j;
}
for (int i = 1, j = 0; i <= m; i++) {
while (j > 0 && (j == n || b[i - 1] != a[j])) j = nex[j];
if (b[i - 1] == a[j]) j++;
if (j == n) {
//开始位置
cout << i-n+1 << endl;
}
}
for (int i = 1; i <= n; i++) cout << nex[i]<<' ';
}
return 0;
}
B Period(HDU1358)
题目要求查找输入字符串所有存在循环元的前缀并返回循环次数(相当于求最小循环元)
设最小循环元长度为k,字符串长度为l,则k,l满足关系式l%(l-k)=0,循环次数为l/(l-k).值得注意的是(l-k)就对应着前后缀匹配上的长度,每次查看长度是否符合关系式即可
#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
char a[1000005]; int n,m,nex[1000005];
int main()
{
while (cin >> n) {
if (!n) break;
m++;
cin >> a;
cout << "Test case #" << m << endl;
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && a[i - 1] != a[j]) j = nex[j];
if (a[i - 1] == a[j]) j++;
//出现循环元
if (j && i % (i - j) == 0) {
cout << i << ' ' << i / (i - j)<<endl;
}
}
cout << endl;
}
return 0;
}
C P4391 [BOI2009]Radio Transmission 无线传输
不需要知道s2,我们可以直接把s1的开头看成s2的开头,设s2’为s2的子串,s1满足:
s1=(s2*k)s2’(k=0,1,2,…)
设s2长为x,s1长为n,则s2=n-next[n]
#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
char a[1000005]; int n,N=1,nex[1000005];
int main()
{
cin >>n>> a;
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && a[i - 1] != a[j]) j = nex[j];
if (a[i - 1] == a[j]) j++;
nex[i] = j;
}
cout << n-nex[n];
return 0;
}
D P1470 [USACO2.3]最长前缀 Longest Prefix
由kmp得出P中每个元素在序列s中出现的位置,再依靠dp求得能拼凑出的最长前缀
#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
char a[205][15];string x,b=""; int N, n, m, nex[1000005], dp[200005] = {1};
vector<int> t[200005];
void kmp(char a[]) {
n = strlen(a);
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && a[i - 1] != a[j]) j = nex[j];
if (a[i - 1] == a[j]) j++;
nex[i] = j;
}
for (int i = 1, j = 0; i <= m; i++) {
while (j > 0 && (j == n || b[i - 1] != a[j])) j = nex[j];
if (b[i - 1] == a[j]) j++;
if (j == n) t[i].push_back(n);
}
}
int main()
{
int i = 0;
while (cin >> a[i])
if (a[i++][0] == '.')break;
//注意b可能不只有一行
while (cin >> x) b += x;
m = b.size();
for (int j = 0; j < i - 1; j++) kmp(a[j]);
for (int i = 1; i <= m; i++) {
for (int j = 0; j < t[i].size(); j++)
if (dp[i - t[i][j]]) dp[i] = 1;
if (dp[i]) N = max(N, i);
}
cout << N;
return 0;
}
E Theme Section (hdu-4763)
S=EAEBE,子字符串E长度所有候选项为(next[m],next[next[m]],…),依次检验候选项是否会存在于字符串S的中间部分,检验成功则返回此时的候选项大小
#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
char a[1000005]; int nex[1000005],m;
bool check(int n) {
//检验在字符串的中间部分是否存在对应的子字符串
for (int i = n+1, j = 0; i <= m-n; i++) {
while (j > 0 && (j == n || a[i - 1] != a[j])) j = nex[j];
if (a[i - 1] == a[j]) j++;
if (j == n) return 1;
}
return 0;
}
int main()
{
std::ios::sync_with_stdio(0);
int n, t; cin >> t;
while (t--) {
cin >> a; m = strlen(a);
nex[1] = 0;
for (int i = 2, j = 0; i <= m; i++) {
while (j > 0 && a[i - 1] != a[j]) j = nex[j];
if (a[i - 1] == a[j]) j++;
nex[i] = j;
}
int N = nex[m];
while (N) {
if (check(N)) break;
//若匹配失败,再利用nex数组得到剩下的最大"候选项"
N = nex[N];
}
cout << N << endl;
}
return 0;
}
F P2375 [NOI2014] 动物园
先求一个前后缀可以重叠的num0数组,再仿照next数组的求解过程,由num0数组得出num数组
#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
char a[1000005]; int n, nex[1000005],num0[1000005], k = 1000000007;
int main()
{
int t; cin >> t;
while (t--) {
cin >> a;
n = strlen(a);
nex[1] = 0; num0[1] = 1;
for (int i = 2, j = 0; i <= n; i++) {
while (j > 0 && a[i - 1] != a[j]) j = nex[j];
if (a[i - 1] == a[j]) j++;
nex[i] = j; num0[i] = num0[j] + 1;
}
int N = 1;
for (int i = 2,j=0; i <= n; i++) {
while (j > 0 && a[i - 1] != a[j]) j = nex[j];
if (a[i - 1] == a[j]) j++;
while (j>i/2) j = nex[j];
N = (long long)(num0[j] + 1) * N % k;
}
cout << N << endl;
}
return 0;
}
G P3808【模板】AC 自动机(简单版)
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int M = 1e6 + 5;
char a[M], t[M]; int q[M], nex[M], cnt[M], tr[M][28], N = 1; bool vis[M];
//字典树
void trie() {
int T; cin >> T;
while (T--) {
cin >> a;
int n = strlen(a), p = 0, ch;
for (int i = 0; i < n; i++) {
ch = a[i] - 'a';
if (!tr[p][ch])tr[p][ch] = ++N;
p = tr[p][ch];
}
cnt[p]++;
}
}
//求"fail"数组
void build() {
int hh = 0, tt = -1;
for (int i = 0; i < 26; i++)
if (tr[0][i]) q[++tt] = tr[0][i];
while (hh <= tt) {
int s = q[hh++];
for (int i = 0; i < 26; i++) {
int p = tr[s][i];
if (!p) tr[s][i] = tr[nex[s]][i];
else {
nex[p] = tr[nex[s]][i];
q[++tt] = p;
}
}
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
trie();
build();
int res = 0; cin >> t;
for (int i = 0, j = 0; t[i]; i++) {
int s = t[i] - 'a';
j = tr[j][s];
int p = j;
//若p已出现过,不再累加
while (p&&!vis[p]) {
res += cnt[p];
cnt[p] = 0; vis[p] = 1;
p = nex[p];
}
}
cout << res;
return 0;
}
H P3796 【模板】AC 自动机(加强版)
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int M = 1e6 + 5;
char a[155][71], t[M]; int T, q[M], nex[M], cnt[M], tr[M][28], N, vis[M];
void init() {
memset(cnt, 0, sizeof(cnt));
memset(vis, 0, sizeof(vis));
memset(tr, 0, sizeof(tr));
memset(nex, 0, sizeof(nex));
N = 1;
}
void trie() {
for (int i = 1; i <= T; i++) {
cin >> a[i];
int n = strlen(a[i]), p = 0, ch;
for (int j = 0; j < n; j++) {
ch = a[i][j] - 'a';
if (!tr[p][ch])tr[p][ch] = ++N;
p = tr[p][ch];
}
//离散化表示各模式串
vis[p] = i;
}
}
void build() {
int hh = 0, tt = -1;
for (int i = 0; i < 26; i++)
if (tr[0][i]) q[++tt] = tr[0][i];
while (hh <= tt) {
int s = q[hh++];
for (int i = 0; i < 26; i++) {
int p = tr[s][i];
if (!p) tr[s][i] = tr[nex[s]][i];
else {
nex[p] = tr[nex[s]][i];
q[++tt] = p;
}
}
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
while (cin >> T) {
if (!T) break;
init();
trie();
build();
cin >> t;
for (int i = 0, j = 0; t[i]; i++) {
int s = t[i] - 'a';
j = tr[j][s];
int p = j;
while (p) {
//给每次出现的模式串++
cnt[vis[p]]++;
p = nex[p];
}
}
int mmax = 0;
for (int i = 1; i <= T; i++) mmax = max(cnt[i], mmax);
cout << mmax << endl;
for (int i = 1; i <= T; i++)
if (mmax == cnt[i])
cout << a[i] << endl;
}
return 0;
}
题目链接:
【模板】KMP字符串匹配
Period
Radio Transmission
最长前缀 Longest Prefix
Theme Section
动物园
AC 自动机(简单版)
AC 自动机(加强版)