【专题】字符串专题小结(AC自动机 + 后缀自动机)
AC自动机相关:
$fail$树:
$fail$树上以最长$border$关系形成父子关系,我们定一个节点对应的串为根到该节点的路径。
对于任意一个非根节点$x$,定$y = fa_{x}$,那$y$对应的串就是$x$对应的串的最长$border$,也就是说如果母串能走到$x$,那母串中一定存在一个子串对应了$y$,而且是当前母串匹配到当前位置的一个后缀。
求每个模式串在母串中出现的次数:
这应该算是AC自动机最基本的问题。
把母串在自动机上跑一遍,显然所有被访问过的节点都是母串的子串,但以当前匹配位置为后缀的模式串可能不仅仅只有一个,还有它所有的$border$。一个很好的性质就是,$fail$树上某个节点的祖先链就恰好完全对应了该节点的所有$border$。我们用$cnt_{i}$表示$i$点的祖先链上有效节点的个数,那只要每次匹配到某个节点$x$时,把它的$cnt_{x}$加上贡献就行了。
如果要支持动态加减模式串,由于修改一个节点的有效性只会对它的子树中所有节点造成影响,只要维护$Dfs$序上的信息即可。
AC自动机上的$Dp$问题
通常情况下,这类$Dp$问题的数据范围不大,设计状态的基本套路通常有两维,一维是母串匹配到的位置,一维是在自动机上的位置,转移基本上都是枚举字符。
$\star$ 一个简单的例子,就是给出一个数$n$和$m$个字符串,问长度为$n$的字符串有多少个满足这$m$个串都是它的子串。题目链接
由于$m$非常小,我们直接状压起来,表示该节点对应的串包含了哪些子串,$Dp$有两种方式,$Dfs$和$Bfs$都可以,如果$Dfs$的话状态的含义通常表示为当前状态到结束状态的方案数。
不过由于这题要输出方案,用$Bfs$的做法不太容易,还是用$Dfs$的吧。
#include <cstdio>
#include <queue>
#include <cstring> using namespace std; typedef long long LL;
const int N = , M = ; int n, m, ST, tot;
char sr[];
int ch[M][], fail[M], fir[M];
queue<int> Q; LL dp[N][M][], ans; void Ins(char *s, int id) {
int pos = ;
for (int i = ; s[i]; ++i) {
int nx = s[i] - 'a';
if (!ch[pos][nx]) {
ch[pos][nx] = ++tot;
memset(ch[tot], , sizeof ch[tot]);
fir[tot] = fail[tot] = ;
}
pos = ch[pos][nx];
}
fir[pos] |= << id;
} void Build() {
Q.push();
for (; !Q.empty(); ) {
int x = Q.front(); Q.pop();
for (int i = ; i < ; ++i) {
int v = ch[x][i];
if (!v) ch[x][i] = ch[fail[x]][i]; else Q.push(v);
if (x && v) fail[v] = ch[fail[x]][i], fir[v] |= fir[fail[v]];
}
}
} LL Dfs(int x, int p, int s) {
if (~dp[x][p][s]) return dp[x][p][s];
if (x == n) {
dp[x][p][s] = (LL) (s == ST);
return dp[x][p][s];
}
dp[x][p][s] = ;
for (int i = ; i < ; ++i) {
int np = ch[p][i];
dp[x][p][s] += Dfs(x + , np, s | fir[np]);
}
return dp[x][p][s];
} void F_(int x, int p, int s) {
if (x == n) {
sr[x] = '\0';
printf("%s\n", sr);
return;
}
for (int i = ; i < ; ++i) {
int np = ch[p][i];
if (dp[x + ][np][s | fir[np]]) {
sr[x] = 'a' + i;
F_(x + , np, s | fir[np]);
sr[x] = '\0';
}
}
} void Clear() {
memset(dp, -, sizeof dp);
memset(ch[], , sizeof ch[]);
tot = ans = ;
} int main() {
for (int tc = ; scanf("%d%d", &n, &m) == && n + m > ; ) {
Clear();
ST = ( << m) - ;
for (int i = ; i < m; ++i) {
scanf("%s", sr);
Ins(sr, i);
}
Build();
ans = Dfs(, , );
printf("Case %d: %lld suspects\n", ++tc, ans);
if (ans <= ) {
memset(sr, , sizeof sr);
F_(, , );
}
} return ;
}
后缀自动机相关:
$parent$树:
$parent$树上以后缀关系形成父子关系,对于任意非根节点有$dep_{i} - dep_{fa_{i}}$为节点$i$包含的子串个数。
求一个串的不重复子串个数:
这是后缀自动机上的一个经典问题,很多时候它都会作为解决一个问题的子问题。事实上这个问题很容易想到,每一个子串都别表现在了自动机上的一个节点,所有相同的子串只会被表现一次,重复的将算在$right$集合中了。每个节点包含的不重复子串个数就是$max_{i} - min_{i} + 1$连续的一段,把所有节点包含的不重复子串数量相加就行了。
$ans = \sum\limits_{i = 1}^{tot} dep_{i} - dep_{fa_{i}}$。
将所有子串按照字典序排序:
由于字典序是比较前缀的,通常情况我们把反串建出后缀自动机,那一个节点上表示的子串都有同样的后缀,对应了原串的前缀,那我们只要对反串进行后缀意义上的排序就可以了。很显然,一个节点中表示的所有子串一定在后缀字典序中是连续的。于是,我们对于$parent$树上每一个节点$x$,假设$y = fa_{x}$,$x$中最短的子串比$y$中最长的子串多的那个字符$a$一定会在$y$中最长串的前一格,那我们就定$son_{y,a} = x$。显然,$son$表示的就是一棵树,这棵树的$Dfs$序就是我们要求的对于每个节点而言的后缀字典序,因为我们在对它进行$Dfs$时会优先选择下一个字符较小的串。
以下给出具体建树的实现:
void Dfs(int t) {
id[++tp] = t;
for (int i = ; i < ; ++i) {
if (son[t][i]) Dfs(son[t][i]);
}
} void Build() {
for (int i = ; i <= tot; ++i) ++bit[dep[i]];
for (int i = ; i <= tot; ++i) bit[i] += bit[i - ];
for (int i = tot; i >= ; --i) id[bit[dep[i]]--] = i;
for (int i = tot; i >= ; --i) {
int x = id[i];
son[fa[x]][s[ed[x] - dep[fa[x]]] - 'a'] = x;
}
Dfs();
}
(注:其中$ed_{i}$表示节点$i$表示的子串末尾字符的位置)
求出字典序第$k$小子串:
此处分两种,一种是算重复子串,一种是不算重复子串,本质上没有区别,如果算重复子串只要对每个节点多乘上$right$集合大小就可以了。
有了上一个模型,我们只要在$Dfs$后的序列上二分就可以了,维护一个前缀的$sum_{i}$表示前$i$个节点表示的子串有多少个,二分可以找到第$k$小子串所在的节点,就能知道是那个子串了。以上是不计重复子串的,如果算上重复子串,由于每个节点上的$right$集合大小是一样的,所以在计算子串个数时乘上$right$集合大小即可,另一个问题,如果算重复子串,那在求出该子串的具体位置的时候一般需要另一个二分确定它的长度。
确定某子串在所有子串中字典序排名:
以$[l,r]$的形式给出一个子串,设$pos_{i}$为第$i$个字符串被插入时新建的节点,由于互为后缀的节点在$parent$树上形成了一条到根的链,显然子串$[l,r]$将会出现在$pos_{r}$的祖先链上,我们就可以用树上倍增找到包含$[l,r]$的节点了。我们可以这么做,是因为子串长度随节点深度递减而单调递减,并且子串长度范围没有交集。
$\star$ 下面的一道题就是上述的有关子串字典序的一个应用,掌握了这个套路就没有什么难度了,仅此以作实例。题目链接
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL;
const int N = ; int n, Qi, tp, gans, ans;
char s[N];
LL sum[N]; int tot, lst, ch[N][], dep[N], fa[N], rig[N];
int id[N], bit[N], ed[N], son[N][];
inline int New_(int _d, int _f, int _r, int _e) {
dep[++tot] = _d; fa[tot] = _f; rig[tot] = _r; ed[tot] = _e;
return tot;
}
inline void Ins(int a, int i) {
int np = New_(dep[lst] + , , , i), p = lst;
lst = np;
for (; p && !ch[p][a]; p = fa[p]) ch[p][a] = np;
if (!p) return void(fa[np] = );
int q = ch[p][a];
if (dep[p] + == dep[q]) return void(fa[np] = q);
int y = New_(dep[p] + , fa[q], , ed[q]);
memcpy(ch[y], ch[q], sizeof ch[y]);
fa[q] = fa[np] = y;
for (; p && ch[p][a] == q; p = fa[p]) ch[p][a] = y;
}
void Build() {
for (int i = ; i <= tot; ++i) ++bit[dep[i]];
for (int i = ; i <= tot; ++i) bit[i] += bit[i - ];
for (int i = tot; i >= ; --i) id[bit[dep[i]]--] = i;
for (int i = tot; i >= ; --i) {
int x = id[i];
rig[fa[x]] += rig[x];
ed[fa[x]] = ed[x]; // a question for why it is used
son[fa[x]][s[ed[x] - dep[fa[x]]] - 'a'] = x;
}
} void Dfs(int t) {
id[++tp] = t;
for (int i = ; i < ; ++i) {
if (son[t][i]) Dfs(son[t][i]);
}
} LL Get(int l, int r) {
if (l > r) return ;
return (LL) (l + r) * (r - l + ) / ;
} int Solve(LL k) {
int nl = , nr = tot, po = -;
for (int md; nl <= nr; ) {
md = (nl + nr) >> ;
if (sum[md] >= k) {
po = md; nr = md - ;
} else {
nl = md + ;
}
}
if (po == -) throw;
k -= sum[po - ];
int L = dep[fa[id[po]]] + , R = dep[id[po]], re = -;
nl = L; nr = R;
for (int md; nl <= nr; ) {
md = (nl + nr) >> ;
if (k <= rig[id[po]] * Get(L, md)) {
re = md; nr = md - ;
} else {
nl = md + ;
}
}
if (re == -) throw;
k -= rig[id[po]] * Get(L, re - );
k = (k - ) % re + ;
return s[ed[id[po]] - k + ];
} int main() {
scanf("%s", s + );
n = strlen(s + );
std::reverse(s + , s + + n);
tot = lst = ;
for (int i = ; s[i]; ++i) Ins(s[i] - 'a', i);
Build();
Dfs();
for (int i = ; i <= tot; ++i) {
sum[i] = sum[i - ] + rig[id[i]] * Get(dep[fa[id[i]]] + , dep[id[i]]);
} scanf("%d", &Qi);
for (LL p, m, k; Qi; --Qi) {
scanf("%lld%lld", &p, &m);
k = gans * p % m + ;
ans = Solve(k);
printf("%c\n", ans);
gans += ans;
} return ;
}
用线段树合并维护每个节点的$right$集合:
很多时候我们先要利用$right$集合的有关信息,可以所有$right$集合的大小加起来是$O(n^{2})$的,可幸的是$right$集合拥有一个重要的优越性质,对于$parent$树上每一个节点,它的$right$集合一定是它父亲的$right$集合的真子集,并且和它的兄弟的$right$集合不相交。于是我们可以用线段树合并来维护,每次从叶子到根把信息合并到父亲上去,需要注意的是我们不想在合并时改变原有的信息,所以合并时需要新建节点,故时空复杂度都是$O(nlogn)$的。
$\star$ 下面的一道题就是有关线段树合并维护$right$集合的应用,具体思想就是二分答案串长,倍增找到子串的节点后询问$right$集合中是否存在。题目链接
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = , LOG = ; int n, m;
int Rt[N], gr[LOG][N];
char s[N]; namespace SE {
const int M = N * ;
int tot, lc[M], rc[M], sum[M];
void Modify(int &t, int l, int r, int x) {
if (!t) t = ++tot;
++sum[t];
if (l == r) return;
int md = (l + r) >> ;
if (x <= md) Modify(lc[t], l, md, x);
else Modify(rc[t], md + , r, x);
}
int Merge(int x, int y) {
if (!x || !y) return x + y;
int z = ++tot;
lc[z] = Merge(lc[x], lc[y]);
rc[z] = Merge(rc[x], rc[y]);
sum[z] = sum[lc[z]] + sum[rc[z]];
return z;
}
int Query(int t, int l, int r, int L, int R) {
int md = (l + r) >> , re = ;
if (L <= l && r <= R) return sum[t];
if (L <= md) re += Query(lc[t], l, md, L, R);
if (R > md) re += Query(rc[t], md + , r, L, R);
return re;
}
} int tot = , lst = , ch[N][], dep[N], fa[N];
int bit[N], id[N], pos[N];
inline int New_(int _dep, int _fa) {
dep[++tot] = _dep; fa[tot] = _fa;
return tot;
}
void Ins(int a, int i) {
int np = New_(dep[lst] + , ), p = lst;
lst = np; pos[i] = tot;
SE::Modify(Rt[tot], , n, i);
for (; p && !ch[p][a]; p = fa[p]) ch[p][a] = np;
if (!p) return void(fa[np] = );
int q = ch[p][a];
if (dep[q] == dep[p] + ) return void(fa[np] = q);
int y = New_(dep[p] + , fa[q]);
memcpy(ch[y], ch[q], sizeof ch[y]);
fa[q] = fa[np] = y;
for (; p && ch[p][a] == q; p = fa[p]) ch[p][a] = y;
}
void Build() {
for (int i = ; i <= tot; ++i) ++bit[dep[i]];
for (int i = ; i <= tot; ++i) bit[i] += bit[i - ];
for (int i = ; i <= tot; ++i) id[bit[dep[i]]--] = i;
for (int i = ; i <= tot; ++i) {
int x = id[i]; gr[][x] = fa[x];
for (int j = ; j < LOG; ++j) gr[j][x] = gr[j - ][gr[j - ][x]];
}
for (int i = tot; i >= ; --i) {
int x = id[i], y = fa[x];
Rt[y] = SE::Merge(Rt[x], Rt[y]);
}
} int Check(int x, int md, int l, int r) {
if (l > r) return ;
for (int i = LOG - ; ~i; --i) {
if (gr[i][x] && dep[gr[i][x]] >= md) x = gr[i][x];
}
return SE::Query(Rt[x], , n, l, r) > ;
} int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + );
reverse(s + , s + + n);
for (int i = ; i <= n; ++i) Ins(s[i] - 'a', i);
Build(); for (int a, b, c, d; m; --m) {
scanf("%d%d%d%d", &a, &b, &c, &d);
a = n - a + ; b = n - b + ; swap(a, b);
c = n - c + ; d = n - d + ; swap(c, d);
int nl = , nr = d - c + , re = ;
for (int md; nl <= nr; ) {
md = (nl + nr) >> ;
if (Check(pos[d], md, a + md - , b)) {
re = md; nl = md + ;
} else {
nr = md - ;
}
}
printf("%d\n", re);
} return ;
}
$\star$ 有了以上的作为基础,我们就能解决下面这个问题了,有两问。
- 给定$k1,k2$,求不重复子串中字典序第$k1$小的子串,并在所有与它相同的子串中找到从左到右第$k2$个,以$[l,r]$的形式返回答案。
- 以$[l,r]$的形式给定子串,求它在所有不重复子串中字典序的排名$k1$,以及在所有与它相同的子串中从左到右的排名$k2$,返回$k1,k2$。
对于第一问:
我们根据$son$树的$Dfs$序可以知道字典序第$k1$小的子串所在的节点$x$,然后我们想要知道$x$的$right$集合的第$k2$个元素在哪个位置,在线段树上二分即可。
对于第二问:
我们在$parent$树上倍增就能找到子串$[l,r]$所在的节点$x$,然后我们想要知道$r$这个位置在$x$的$right$集合中排第几,在线段树上二分即可。
我想要说的是,有关字典序、子串等的问题都是比较套路的问题,掌握技巧就可以了。
这是这个问题的相关实现:原题地址
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL;
const int N = , LOG = ; int n, m, tp;
int Rt[N], gr[LOG][N];
char s[N], ssr[];
LL sum[N]; inline void Read(LL &x) {
x = ; static char c;
for (c = getchar(); c < '' || c > ''; c = getchar());
for (; c >= '' && c <= ''; x = (x << ) + (x << ) + c - '', c = getchar());
} namespace SE {
const int M = N * ;
int tot, lc[M], rc[M], sum[M];
void Modify(int &t, int l, int r, int x) {
if (!t) t = ++tot;
++sum[t];
if (l == r) return;
int md = (l + r) >> ;
if (x <= md) Modify(lc[t], l, md, x);
else Modify(rc[t], md + , r, x);
}
int Merge(int x, int y) {
if (!x || !y) return x + y;
int z = ++tot;
lc[z] = Merge(lc[x], lc[y]);
rc[z] = Merge(rc[x], rc[y]);
sum[z] = sum[lc[z]] + sum[rc[z]];
return z;
}
int Query_1(int t, int l, int r, int k) {
if (l == r) return l;
int md = (l + r) >> ;
if (sum[lc[t]] >= k) return Query_1(lc[t], l, md, k);
return Query_1(rc[t], md + , r, k - sum[lc[t]]);
}
int Query_2(int t, int l, int r, int k) {
if (l == r) return sum[t];
int md = (l + r) >> ;
if (k <= md) return Query_2(lc[t], l, md, k);
return sum[lc[t]] + Query_2(rc[t], md + , r, k);
}
} int tot = , lst = , ch[N][], dep[N], rig[N], fa[N], ed[N];
int bit[N], id[N], rk[N], pos[N], son[N][];
inline int New_(int _dep, int _fa, int _ri, int _e) {
dep[++tot] = _dep; fa[tot] = _fa; rig[tot] = _ri; ed[tot] = _e;
return tot;
}
void Ins(int a, int i) {
int np = New_(dep[lst] + , , , i), p = lst;
lst = np; pos[i] = tot;
SE::Modify(Rt[tot], , n, i);
for (; p && !ch[p][a]; p = fa[p]) ch[p][a] = np;
if (!p) return void(fa[np] = );
int q = ch[p][a];
if (dep[q] == dep[p] + ) return void(fa[np] = q);
int y = New_(dep[p] + , fa[q], , ed[q]);
memcpy(ch[y], ch[q], sizeof ch[y]);
fa[q] = fa[np] = y;
for (; p && ch[p][a] == q; p = fa[p]) ch[p][a] = y;
}
void Dfs(int x) {
id[++tp] = x;
for (int i = ; i < ; ++i) {
if (son[x][i]) Dfs(son[x][i]);
}
}
void Build() {
for (int i = ; i <= tot; ++i) ++bit[dep[i]];
for (int i = ; i <= tot; ++i) bit[i] += bit[i - ];
for (int i = ; i <= tot; ++i) id[bit[dep[i]]--] = i;
for (int i = ; i <= tot; ++i) {
int x = id[i]; gr[][x] = fa[x];
for (int j = ; j < LOG; ++j) gr[j][x] = gr[j - ][gr[j - ][x]];
}
for (int i = tot; i >= ; --i) {
int x = id[i], y = fa[x];
rig[y] += rig[x];
Rt[y] = SE::Merge(Rt[x], Rt[y]);
son[y][s[ed[x] - dep[y]] - 'a'] = x;
}
Dfs();
if (tp != tot) { cerr << "not tp equal!" << endl; throw; }
for (int i = ; i <= tot; ++i) {
rk[id[i]] = i;
sum[i] = sum[i - ] + dep[id[i]] - dep[fa[id[i]]];
}
} pair<LL, LL> Solve_1(LL k1, int k2) {
int nl = , nr = tot, x = -;
for (int md; nl <= nr; ) {
md = (nl + nr) >> ;
if (k1 <= sum[md]) {
x = md; nr = md - ;
} else {
nl = md + ;
}
}
if (x == -) { cerr << "not found po!" << endl; throw; }
k1 -= sum[x - ];
x = id[x];
if (k2 > rig[x]) { cerr << "k2" << endl; throw; }
k2 = rig[x] - k2 + ;
int ps = SE::Query_1(Rt[x], , n, k2);
int len = k1 + dep[fa[x]];
return make_pair(ps - len + , ps);
}
pair<LL, LL> Solve_2(int l, int r) {
int x = pos[r], le = r - l + ;
for (int i = LOG - ; ~i; --i) {
if (gr[i][x] && dep[gr[i][x]] >= le) x = gr[i][x];
}
int re = SE::Query_2(Rt[x], , n, r);
LL rnk = sum[rk[x] - ] + le - dep[fa[x]];
return make_pair(rnk, rig[x] - re + );
} int main() {
scanf("%s", s + );
n = strlen(s + );
reverse(s + , s + + n);
for (int i = ; i <= n; ++i) Ins(s[i] - 'a', i);
Build(); scanf("%d", &m);
pair<LL, LL> re;
for (LL l, r; m; --m) {
scanf("%s", ssr);
Read(l); Read(r);
if (ssr[] == 'S') {
re = Solve_1(l, r);
re.first = n - re.first + ;
re.second = n - re.second + ;
swap(re.first, re.second);
} else {
l = n - l + ; r = n - r + ; swap(l, r);
re = Solve_2(l, r);
}
printf("%lld %lld\n", re.first, re.second);
} return ;
}
求两子串的$LCS$($LCP$):
子串的最长公共后缀在$parent$树上是一个经典问题,我们已经知道了如何找到一个子串所在的节点,那么答案节点就是两个子串所在节点在$parent$树上的$LCA$。
对于$LCP$,只要对反串做就行了。
后缀自动机在区间上的单调性问题:
通常情况下,有关后缀的区间问题总是会有单调性,就是子串的存在性。我们的区间就是子串,显然,如果一个区间是某个串的子串,那它的子区间就一定是该串的子串。
我们来看一个简单的问题,给出串$S,T$,询问$S$中有多少子串是$T$的子串。
根据上述的单调性,对于每一个作为子串右端点的位置$i$,都存在一个极左的位置$l_{i}$满足所有比$l_{i}$小的位置都不合法,所有大于等于$l_{i}$的位置都合法,所以用$two \; pointers$能解决问题。我们往往用自动机上的一个节点$p$表示当前我们维护的区间(子串),每次$nr$准备向右移一格时,相当于在$p$上尝试转移,如果存在转移边,那$[nl,nr+1]$也就是说一个合法的子串,$nl$不动;否则就需要让$nl$右移一格,这就相当于变成了比$[nl,nr+1]$恰好短一的一个后缀。这里有注意的地方就是,当每次短一时有可能串长减小到它父亲的长度范围,也就是此时串长$len = dep_{fa_{p}}$,这时要让$p$跳一次父亲。
$\star$ 这里给出一道有关单调性的问题。题目链接
可以观察到,我们进行复制操作时,设$j$为复制的起点,则$[j + 1, i]$必须是$[1,j]$的子串,而这个是存在上述我们讨论的单调性的。
可以列出$Dp$的方程:$dp_{i} = max_{j = k}^{i - 1} \; \{ \; dp_{j} + (i - j) * A + 2 * B \; \}$,以及和$dp_{i - 1} + cost_{s_{i}}$取最小值,其中$k$为最小的能作为起点的位置。
要求出每一个$i$的$k$,可以用上述的方法维护,只不过当前的自动机只有$[1,k]$而已,要支持动态插入字符,可能会改变$p$的节点位置,所以在维护$p$时记得更新。
那我们只要维护区间最值就行了,这里用由于$k$是单调递增的,用单调队列维护就行了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL;
const int N = ; int tc, n, kp, le;
char s[N];
int q[N], cost[];
LL dp[N], A, B; int tot, lst, ch[N][], dep[N], fa[N];
inline int New_(int _dep, int _fa) {
dep[++tot] = _dep; fa[tot] = _fa;
memset(ch[tot], , sizeof ch[tot]);
return tot;
}
inline void Ins(int a) {
int np = New_(dep[lst] + , ), p = lst;
lst = np;
for (; p && !ch[p][a]; p = fa[p]) ch[p][a] = np;
if (!p) return void(fa[np] = );
int q = ch[p][a];
if (dep[p] + == dep[q]) return void(fa[np] = q);
int y = New_(dep[p] + , fa[q]);
memcpy(ch[y], ch[q], sizeof ch[y]);
fa[q] = fa[np] = y;
if (kp == q && le <= dep[y]) kp = y;
for (; p && ch[p][a] == q; p = fa[p]) ch[p][a] = y;
} void Clear() {
memset(ch[], , sizeof ch[]);
tot = lst = ;
} int main() {
scanf("%d", &tc);
for (int tm = ; tm <= tc; ++tm) {
Clear();
scanf("%s", s + );
n = strlen(s + );
for (int i = ; i < ; ++i) {
scanf("%d", &cost[i]);
}
scanf("%lld%lld", &A, &B);
int nl = , nr = ; kp = ;
for (int i = , j = ; i <= n; ++i) {
int nc = s[i] - 'a';
dp[i] = dp[i - ] + cost[nc];
while (j <= i && !ch[kp][nc]) {
if (i - j - == dep[fa[kp]]) kp = fa[kp];
le = i - j - ;
Ins(s[j] - 'a'); ++j;
}
if (j <= i) kp = ch[kp][nc];
while (nl <= nr && q[nl] < j - ) ++nl;
if (nl <= nr) dp[i] = min(dp[i], dp[q[nl]] + (i - q[nl]) * A + * B);
while (nl <= nr && dp[q[nr]] - q[nr] * A >= dp[i] - i * A) --nr;
q[++nr] = i;
}
printf("Case #%d: %lld\n", tm, dp[n]);
} return ;
}
【专题】字符串专题小结(AC自动机 + 后缀自动机)的更多相关文章
- bzoj 3796: Mushroom追妹纸 AC自动机+后缀自动机+dp
题目大意: 给定三个字符串s1,s2,s3,求一个字符串w满足: w是s1的子串 w是s2的子串 s3不是w的子串 w的长度应尽可能大 题解: 首先我们可以用AC自动机找出s3在s1,s2中出现的位置 ...
- BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机/后缀自动机)
Description a180285幸运地被选做了地球到喵星球的留学生.他发现喵星人在上课前的点名现象非常有趣. 假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成.喵星球上的老师会选择M个串 ...
- AC自动机&后缀自动机
理解的不够深 故只能以此来加深理解 .我这个人就是蠢没办法 学长讲的题全程蒙蔽.可能我字符串就是菜吧,哦不我这个人就是菜吧. AC自动机的名字 AC 取自一个大牛 而自动机就比较有讲究了 不是寻常的东 ...
- BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机 后缀自动机 字符串
https://www.lydsy.com/JudgeOnline/problem.php?id=3926 广义后缀自动机是一种可以处理好多字符串的一种数据结构(不像后缀自动机只有处理一到两种的时候比 ...
- BZOJ 3998: [TJOI2015]弦论 后缀自动机 后缀自动机求第k小子串
http://www.lydsy.com/JudgeOnline/problem.php?id=3998 后缀自动机应用的一个模板?需要对len进行一个排序之后再统计每个出现的数量,维护的是以该字符串 ...
- BZOJ4032[HEOI2015]最短不公共子串——序列自动机+后缀自动机+DP+贪心
题目描述 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列”指的是它的可以 ...
- CCF NOI Online 2021 提高组 T2 积木小赛 (子序列自动机+后缀自动机,O(n^2))
题面 Alice 和 Bob 最近热衷于玩一个游戏--积木小赛. Alice 和 Bob 初始时各有 n 块积木从左至右排成一排,每块积木都被标上了一个英文小写字母. Alice 可以从自己的积木中丢 ...
- 字符串[未AC](后缀自动机):HEOI 2016 str
超级恶心,先后用set维护right,再用主席树维护,全部超时,本地测是AC的.放心,BZOJ上还是1S限制,貌似只有常数优化到一定境界的人才能AC吧. 总之我是精神胜利了哦耶QAQ #include ...
- HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机
https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...
随机推荐
- 4. 为HelloWorld添加日志
回顾 通过上篇内容,我们已经使用flask编写了我们的第一个接口或者说是html页面.我们可以看到,使用flask来编写接口/页面是十分简单的.那么接下来,我们丰富一下上面的例子. 需求 现在的需求来 ...
- 新手Python第一天(接触)
Python 变量 Python的变量由字母,数字,下划线组成不包含特殊字符,不能以数字开头 可以使用的名称 例如:name,name2,my_name 不可使用的名称 例如:if...(Python ...
- 2019第十届蓝桥杯 E题 迷宫
/*输入 30 50 01010101001011001001010110010110100100001000101010 00001000100000101010010000100000001001 ...
- “Hello World!”Final发布文案加美工
文案: 大家好,我们是“Hello World!”团队,本次我将向大家简要介绍一下空天猎的final发布,在空天猎final发布中,我主要从以下两个方面向大家进行介绍,第一个方面是增加了敌方的boss ...
- vs2013 CodeLens
那东西叫 CodeLens 只有VS2013 旗舰版 (update 2及以上) 才可以用,高级版 专业版都没有.如何打开CodeLens呢?在VS菜单栏 >> 工具 >> ...
- WPF四则运算《《《《《策略模式
设计思路: 因为之前没有用过WPF,听说和window窗体语法类似,就想着仿照之前的Window窗体做的,首先用三个textbox存储数据,添加一个comboBox,利用索引选择运 ...
- Linux adduser
- MiniUI框架合并单元格
在项目中遇到合并单元格的问题,所以总结一下. 用的是miniUI框架,所以只谈miniUI中的单元格合并. (1)必须添加onLoad="onLoad" (2)需要在JS中进行单元 ...
- Window下JDK安装教程
1.准备 win10系统,其他windows系统安装过程大同小异官网下载jdk1.8下载地址:https://www.oracle.com/technetwork/java/javase/downlo ...
- [51CTO]给您介绍Windows10各大版本之间区别
给您介绍Windows10各大版本之间区别 随着win10的不断普及和推广,越来越多的朋友想安装win10系统了,但是很多朋友不知道win10哪个版本好用,为了让大家能够更好的选择win10系统版本, ...