AC 自动机 DP 模板
以及矩阵快速幂优化线性递推 DP。
本篇主要讲矩阵快速幂优化线性递推 DP。
题目
题意
给 个由小写英文字母构成的字符串 ,确定在长度不超过 的字符串 满足 的任意子串都不等于 的数量。
答案对 取模。
输入
op n m
S_1
...
S_m输出
ans数据范围
Subtask 1:
记 trie 树上状态数为 。
由 AC 自动机可以求出哪些状态是合法的。
考虑 DP,定义 表示当长度为 在 AC 自动机匹配状态为 的方案数。
的最后一个字母为 :
注意:当 AC 自动机求失配指针时遇到不存在的状态 时,请将 。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const ll maxn=1e6+5;
int trie[maxn][30],fail[maxn];
int cnt;
int bad[maxn];
void insert(string s) {
ll now=0;
for (char c:s) {
ll ch=c-'a';
if (!trie[now][ch]) trie[now][ch]=++cnt;
now=trie[now][ch];
}
bad[now]=1;
}
void build() {
queue<ll> q;
for (ll i=0;i<26;i++) if (trie[0][i]) q.push(trie[0][i]);
while (!q.empty()) {
ll now=q.front();q.pop();
bad[now]|=bad[fail[now]];
for (ll i=0;i<26;i++) {
if (trie[now][i]) {
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}
else trie[now][i]=trie[fail[now]][i];
}
}
}
void solve() {
ll op,n,m;
cin>>op>>n>>m;
for (ll i=1;i<=m;i++) {
string s;cin>>s;
insert(s);
}
build();
ll sz=cnt;
vector<ll> dp(sz+1,0);
dp[0]=1;
ll ans=0;
for (ll len=1;len<=n;len++) {
vector<ll> ndp(sz+1,0);
for (ll u=0;u<=sz;u++) {
if (!dp[u]||bad[u]) continue;
for (ll c=0;c<26;c++) {
ll v=trie[u][c];
if (!bad[v]) ndp[v]=(ndp[v]+dp[u])%mod;
}
}
dp=ndp;
for (ll u=0;u<=sz;u++) ans=(ans+dp[u])%mod;
}
cout<<ans<<"\n";
}
int main() {
ios::sync_with_stdio(0);
ll t=1;
while (t--) solve();
}Subtask 2:矩阵快速幂优化
每次转移只依赖上一轮,是线性递推。
转移矩阵 :
其实 就是 trie 树上从 到 的边数( 或 ):
具体矩阵转移:
设状态向量 长度为 :
于是:
暴力 DP 每轮累加求出答案,而矩阵版需要一次算出。
扩充矩阵,多一行一列。
设扩充矩阵 :
- :保持原转移。
- : 的所有出度,即当前步 产生的合法串数,累加到
ans行。 - :保留之前累加的
ans。
具体转移:
最后一行展开:
初始值:
的最后一行的值就是答案。
最后矩阵快速幂优化即可从 变为 。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const ll maxn=1e6+5;
int trie[maxn][30],fail[maxn];
int cnt;
int bad[maxn];
void insert(string s) {
ll now=0;
for (char c:s) {
ll ch=c-'a';
if (!trie[now][ch]) trie[now][ch]=++cnt;
now=trie[now][ch];
}
bad[now]=1;
}
void build() {
queue<ll> q;
for (ll i=0;i<26;i++) if (trie[0][i]) q.push(trie[0][i]);
while (!q.empty()) {
ll now=q.front();q.pop();
bad[now]|=bad[fail[now]];
for (ll i=0;i<26;i++) {
if (trie[now][i]) {
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}
else trie[now][i]=trie[fail[now]][i];
}
}
}
struct nd {
ll n;
vector<vector<ll>> a;
nd(ll _n) {
n=_n;
a.resize(n,vector<ll>(n,0));
}
nd operator * (const nd &b) const {
nd res(n);
for (ll i=0;i<n;i++)
for (ll k=0;k<n;k++) if (a[i][k])
for (ll j=0;j<n;j++) if (b.a[k][j])
res.a[i][j]=(res.a[i][j]+a[i][k]*b.a[k][j])%mod;
return res;
}
};
nd qpow(nd a,ll b) {
nd res(a.n);
for (ll i=0;i<a.n;i++) res.a[i][i]=1;
while (b) {
if (b&1) res=res*a;
a=a*a;b>>=1;
}
return res;
}
void solve() {
ll op,n,m;
cin>>op>>n>>m;
for (ll i=1;i<=m;i++) {
string s;cin>>s;
insert(s);
}
build();
ll sz=cnt+1;
nd M(sz+1);
for (ll u=0;u<sz;u++) {
if (bad[u]) continue;
ll sum=0;
for (ll c=0;c<26;c++) {
ll v=trie[u][c];
if (!bad[v]) {
M.a[v][u]++;
sum++;
}
}
M.a[sz][u]=sum;
}
M.a[sz][sz]=1;
M=qpow(M,n);
cout<<M.a[sz][0]<<endl;
}
int main() {
ios::sync_with_stdio(0);
ll t=1;
while (t--) solve();
}