奶龙学习笔记之字符串 Ⅵ
PrinceEvan · · 个人记录
KMP
理论
KMP 用于字符串匹配,如计算字符串
KMP 核心思想为减少不必要的匹配以减少时间复杂度,具体可通过
预处理时,我们先对字符串
::::success[P3375 【模板】KMP 代码]
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int n, m, nxt[maxn];
char s1[maxn], s2[maxn];
int main() {
cin >> (s1 + 1) >> (s2 + 1);
n = strlen(s1 + 1);
m = strlen(s2 + 1);
for(int i = 2, j = 0; i <= m; i++) {
while(j && s2[j + 1] != s2[i]) j = nxt[j];
if(s2[j + 1] == s2[i]) j++;
nxt[i] = j;
}
for(int i = 1, j = 0; i <= n; i++) {
while(j && s2[j + 1] != s1[i]) j = nxt[j];
if(s2[j + 1] == s1[i]) j++;
if(j == m) {
cout << i - m + 1 << "\n";
j = nxt[j];
}
}
for(int i = 1; i <= m; i++) cout << nxt[i] << " ";
cout << "\n";
return 0;
}
::::
时间复杂度分析
在预处理 j++ 最多执行 for 内可能会是时间复杂度增加的时是 while,但 while 内 j = nxt[j] 每执行一次,while 总共最多被执行
例题
P5256 [JSOI2013] 编程作业
题目链接
匹配时大写字母必须相同,同种小写字母必须对应同种小写字母。所以可以想到把当前小写字母修改为 该小写字母距离上一次出现的位置(如果第一次出现就设为该字母下标)。然后就可以正常 KMP 了。注意小写字母距离上一次出现位置要与已匹配长度取最小值。
::::success[代码]
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int T, n, m, a[maxn], b[maxn];
int nxt[maxn], last[30];
char t[maxn];
void read(int &len, int *s) {
cin >> (t + 1);
len = strlen(t + 1);
for(int i = 0; i < 26; i++) last[i] = 0;
for(int i = 1; i <= len; i++) {
if(t[i] < 'a') s[i] = -t[i];
else {
int c = t[i] - 'a';
s[i] = i - last[c];
last[c] = i;
}
}
return;
}
bool check(int x, int y, int s) {
if(x == y) return 1;
return x > s && y > s;
}
int main() {
cin >> T;
while(T--) {
read(n, a);
read(m, b);
for(int i = 2, j = 0; i <= m; i++) {
while(j && !check(b[j + 1], b[i], j)) j = nxt[j];
if(check(b[j + 1], b[i], j)) j++;
nxt[i] = j;
}
int ans = 0;
for(int i = 1, j = 0; i <= n; i++) {
while(j && !check(b[j + 1], a[i], j)) j = nxt[j];
if(check(b[j + 1], a[i], j)) j++;
if(j == m) ans++, j = nxt[j];
}
cout << ans << "\n";
}
return 0;
}
::::
P10634 BZOJ2372 music
题目链接
上一题的升级版,添加了相对大小相同的要求。沿着上一题的想法,我们只需要再次修改判断字符相等的函数。具体的,设两段旋律分别匹配到了第
::::success[代码]
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int len[3], l, d[3][maxn], nxt[maxn], last[30];
int cur[3][maxn], rec[3][maxn][30]; // i 后个数字出现顺序
int cnt[3], now[3][maxn]; // 该字符串前 i 位有多少种数字
void read(int id) {
for(int i = 1; i <= l; i++) last[i] = 0;
int t;
for(int i = 1; i <= len[id]; i++) {
cin >> t;
for(int j = last[t] + 1; j <= i; j++) rec[id][j][++cur[id][j]] = t;
if(!last[t]) now[id][i]++;
d[id][i] = i - last[t];
last[t] = i;
now[id][i] += now[id][i - 1];
}
return;
}
bool check(int id, int k1, int k2) {
// id : 被匹配串编号
int len = k2 - 1; // 已匹配长度
int x = d[id][k1], y = d[2][k2];
bool f1 = (x <= len), f2 = (y <= len);
if(f1 & f2) return (d[id][k1] == d[2][k2]);
if(f1 ^ f2) return 0;
int lim = now[2][len] + 1;
int st = k1 - len;
for(int i = 1; i < lim; i++) {
int u = rec[id][st][i], v = rec[2][1][i];
if((rec[id][st][lim] > u) ^ (rec[2][1][lim] > v)) return 0;
}
return 1;
}
int main() {
cin >> len[1] >> len[2] >> l;
read(1), read(2);
for(int i = 2, j = 0; i <= len[2]; i++) {
while(j && !check(2, i, j + 1)) j = nxt[j];
if(check(2, i, j + 1)) j++;
nxt[i] = j;
}
vector <int> ans;
for(int i = 1, j = 0; i <= len[1]; i++) {
while(j && !check(1, i, j + 1)) j = nxt[j];
if(check(1, i, j + 1)) j++;
if(j == len[2]) {
ans.emplace_back(i - len[2] + 1);
j = nxt[j];
}
}
cout << ans.size() << "\n";
for(int i : ans) cout << i << "\n";
return 0;
}
::::
AC 自动机
理论
AC 自动机就是把 KMP 推广到多个模式串上,思想与 KMP 有相似之处。具体地,先对模式串建 Trie 树(注意 Trie 树的每个节点是一个字符串,边存储的字符),然后求出每个模式串的每个前缀的最长公共前后缀,但是与 KMP 不同的是,匹配时可以匹配到其他字符串上,记作
优化
假设我们要统计每个模式串出现了几次,代码如下:
for(int i = 1; i <=len; i++) {
p = tr[p][s[i] - 'a'];
for(int j = p; j; j = fail[j]) buc[e[j]]++;
}
内层跳
::::success[P5357 【模板】AC 自动机 代码]
#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
const int maxn = 2e5 + 5, maxs = 2e6 + 5;
int n, key[maxn], ans[maxn], deg[maxn];
char s[maxs];
int cnt, tr[maxs][26], fail[maxs], e[maxs];
void insert(int id) {
int p = 0, len = strlen(s + 1);
for(int i = 1; i <= len; i++) {
int c = s[i] - 'a';
if(!tr[p][c]) tr[p][c] = ++cnt;
p = tr[p][c];
}
if(!e[p]) e[p] = id;
key[id] = p;
}
void build() {
queue <int> q;
for(int i = 0; i < 26; i++) if(tr[0][i]) q.push(tr[0][i]);
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = 0; i < 26; i++) {
if(tr[x][i]) {
fail[tr[x][i]] = tr[fail[x]][i];
deg[tr[fail[x]][i]]++;
q.push(tr[x][i]);
}
else tr[x][i] = tr[fail[x]][i];
}
}
}
void topo() {
queue <int> q;
for(int i = 1; i <= cnt; i++) if(!deg[i]) q.push(i);
while(!q.empty()) {
int x = q.front(); q.pop();
int y = fail[x];
deg[y]--;
ans[y] += ans[x];
if(!deg[y]) q.push(y);
}
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%s", s + 1), insert(i);
build();
scanf("%s", s + 1);
int p = 0, len = strlen(s + 1);
for(int i = 1; i <= len; i++) ans[p = tr[p][s[i] - 'a']]++;
topo();
for(int i = 1; i <= n; i++) printf("%d\n", ans[key[i]]);
return 0;
}
::::
例题
P14363 [CSP-S 2025] 谐音替换
题目链接
trick: 字符串的题可以通过添加一些特殊字符重新构造字符串。
题目中的替换与两个字符串有关,但 AC 自动机解决的是多模式串匹配,所以考虑如何将两个字符串合并到一起。
观察替换的性质后不难想到可以将 x+y+z 和 x+y'+z 替换为 x+'#'+y+y'+'#'+z。对于
::::success[代码]
#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
const int maxn = 2e5 + 5, maxl = 5e6 + 5;
int n, q, l[2][maxn];
char s1[maxl], s2[maxl], s[maxl];
int cnt, tr[maxl][27], fail[maxl], e[maxl];
void merge(int &l0, int &l1) {
l0 = strlen(s1 + 1);
int x = 1, z = l0;
while(x < l0 && s1[x] == s2[x]) x++;
while(z > 1 && s1[z] == s2[z]) z--;
for(int i = 1; i < x; i++) s[++l1] = s1[i];
s[++l1] = 'z' + 1;
for(int i = x; i <= z; i++) s[++l1] = s1[i];
for(int i = x; i <= z; i++) s[++l1] = s2[i];
s[++l1] = 'z' + 1;
for(int i = z + 1; i <= l0; i++) s[++l1] = s2[i];
}
void insert(int len) {
int p = 0;
for(int i = 1; i <= len; i++) {
int c = s[i] - 'a';
if(!tr[p][c]) tr[p][c] = ++cnt;
p = tr[p][c];
}
e[p]++;
}
void build() {
queue <int> q;
for(int i = 0; i < 27; i++) if(tr[0][i]) q.push(tr[0][i]);
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = 0; i < 27; i++) {
if(tr[x][i]) {
fail[tr[x][i]] = tr[fail[x]][i];
e[tr[x][i]] += e[tr[fail[x]][i]];
q.push(tr[x][i]);
}
else tr[x][i] = tr[fail[x]][i];
}
}
}
int main() {
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++) {
scanf("%s%s", s1 + 1, s2 + 1);
merge(l[0][i], l[1][i]), insert(l[1][i]);
}
build();
while(q--) {
scanf("%s%s", s1 + 1, s2 + 1);
int len = strlen(s1 + 1);
if(len != strlen(s2 + 1)) {
puts("0");
continue;
}
int l0 = 0, l1 = 0, p = 0, ans = 0;
merge(l0, l1);
for(int i = 1; i <= l1; i++) ans += e[p = tr[p][s[i] - 'a']];
printf("%d\n", ans);
}
return 0;
}
::::
P2444 [POI 2000 R1] 病毒
题目链接
很容易想到这道题就是判 AC 自动机上有没有环,并且这个环上不能有任何一点的字符串包含了病毒代码。
但是还有一些细节需要想清楚:
-
如果一个点的
fail 指向的节点的字符串是病毒代码,那么这个节点也是不可访问的。 -
在 dfs 判环的过程中,我们不需要走
fail 指针连的边,因为在建fail 树的过程中如果当前节点并没有儿子为i ,我们就将他的i 儿子设为了tr[fail[x]][i],这就等价于考虑了它父亲的fail 边。
::::success[代码]
#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
const int maxn = 2e3 + 5, maxs = 3e4 + 5;
int n, fail[maxs];
int cnt, tr[maxs][2];
char s[maxs];
bool cyc[maxs], vis[maxs], ed[maxs];
void insert() {
int p = 0, len = strlen(s + 1);
for(int i = 1; i <= len; i++) {
int c = s[i] - '0';
if(!tr[p][c]) tr[p][c] = ++cnt;
p = tr[p][c];
}
ed[p] = 1;
}
void build() {
queue <int> q;
if(tr[0][0]) q.push(tr[0][0]);
if(tr[0][1]) q.push(tr[0][1]);
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = 0; i < 2; i++) {
if(tr[x][i]) {
fail[tr[x][i]] = tr[fail[x]][i];
ed[tr[x][i]] |= ed[fail[tr[x][i]]];
q.push(tr[x][i]);
}
else tr[x][i] = tr[fail[x]][i];
}
}
}
void dfs(int x) {
if(cyc[x]) {
puts("TAK");
exit(0);
}
if(ed[x] | vis[x]) return;
vis[x] = cyc[x] = 1;
dfs(tr[x][0]), dfs(tr[x][1]);
cyc[x] = 0;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%s", s + 1);
insert();
}
build(), dfs(0);
puts("NIE");
return 0;
}
::::
P2414 [NOI2011] 阿狸的打字机
代码链接
trick:
-
Trie 树上是前缀关系:节点
x 的祖先的字符串都是x 的字符串的前缀。 -
有了上面的 trick 我们就可以把字串问题转化为前后缀问题了。设节点
这个东西显然可以用数据结构维护,但是在线做法可能要用到树剖、主席树等较难写的数据结构,所以考虑离线。先对
::::success[代码]
#include <bits/stdc++.h>
#define pii pair <int, int>
#define mp make_pair
#define eb emplace_back
using namespace std;
const int maxn = 1e5 + 5;
int m, ans[maxn], tot, key[maxn];
vector <pii> qry[maxn];
char str[maxn];
int cnt, tr[maxn][26], rec[maxn][26], fail[maxn];
vector <int> e[maxn];
int fa[maxn], tim, dfn[maxn], siz[maxn];
struct BIT {
int tr[maxn];
void add(int x, int k) {
for(; x <= cnt; x += (x & -x)) tr[x] += k;
return;
}
int qry(int x) {
int res = 0;
for(; x; x -= (x & -x)) res += tr[x];
return res;
}
} bit;
void insert() {
int p = 0, len = strlen(str + 1);
for(int i = 1; i <= len; i++) {
if(str[i] == 'B') p = fa[p];
else if(str[i] == 'P') key[++tot] = p;
else {
int c = str[i] - 'a';
if(!tr[p][c]) fa[rec[p][c] = tr[p][c] = ++cnt] = p;
p = tr[p][c];
}
}
}
void build() {
queue <int> q;
for(int i = 0; i < 26; i++) if(tr[0][i]) q.push(tr[0][i]);
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = 0; i < 26; i++) {
if(tr[x][i]) {
fail[tr[x][i]] = tr[fail[x]][i];
q.push(tr[x][i]);
}
else tr[x][i] = tr[fail[x]][i];
}
}
}
void dfs_fail(int x) {
if(x) dfn[x] = ++tim, siz[x] = 1;
for(int y : e[x]) dfs_fail(y), siz[x] += siz[y];
}
void dfs_trie(int x) {
for(pii i : qry[x]) {
int u = i.first;
int l = dfn[u], r = dfn[u] + siz[u] - 1;
ans[i.second] = bit.qry(r) - bit.qry(l - 1);
}
for(int i = 0; i < 26; i++) {
int y = rec[x][i];
if(!y) continue;
bit.add(dfn[y], 1);
dfs_trie(y);
bit.add(dfn[y], -1);
}
}
int main() {
scanf("%s", str + 1);
insert(), build();
for(int i = 1; i <= cnt; i++) e[fail[i]].eb(i);
dfs_fail(0);
scanf("%d", &m);
int x, y;
for(int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
qry[key[y]].eb(mp(key[x], i));
}
dfs_trie(0);
for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
::::
Manacher
理论
理解后会发现和 KMP、AC 自动机的理论差不多,核心思想都是借助前面求出的信息。但是 Manacher 还多了一步构造字符串。设
::::success[P3805 【模板】Manacher 代码]
#include <bits/stdc++.h>
using namespace std;
const int maxn = 11e6 + 5;
int n, p[maxn << 1];
char s[maxn], t[maxn << 1];
int main() {
scanf("%s", s + 1);
int len = strlen(s + 1);
t[0] = '@', t[++n] = '#';
for(int i = 1; i <= len; i++) t[++n] = s[i], t[++n] = '#';
int k = 0;
for(int i = 1; i <= n; i++) {
if(i < k + p[k]) p[i] = min(p[k * 2 - i], k + p[k] - i);
else p[i] = 1;
while(t[i - p[i]] == t[i + p[i]]) p[i]++;
if(k + p[k] < i + p[i]) k = i;
}
int res = 0;
for(int i = 1; i <= n; i++) res = max(res, p[i] - 1);
printf("%d\n", res);
return 0;
}
::::