POJ 2778 DNA Sequence (AC自动机+DP+矩阵)

时间:2021-12-04 19:12:21

题意:给定一些串,然后让你构造出一个长度为 m 的串,并且不包含以上串,问你有多少个。

析:很明显,如果 m 小的话 ,直接可以用DP来解决,但是 m 太大了,我们可以认为是在AC自动机图中,根据离散中的矩阵的幂可以表示 从 i 到 j 需要 x 步的有多少条。比如A[1][2]^5 = 10,表示从结点 1 到结点 2 走五步有10种方法,利用这种方法,就可以直接进行矩阵快速幂了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const int mod = 100000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
const int maxnode = 10 * 10 + 10;
const int sigma = 4; struct Matrix{
int n;
int a[maxnode][maxnode];
void clear(){ ms(a, 0); }
friend Matrix operator * (const Matrix &lhs, const Matrix &rhs){
Matrix res; res.n = lhs.n;
FOR(i, 0, lhs.n) for(int j = 0; j < lhs.n; ++j){
res.a[i][j] = 0;
for(int k = 0; k < lhs.n; ++k)
res.a[i][j] = (res.a[i][j] + (LL)lhs.a[i][k] * rhs.a[k][j]) % mod;
}
return res;
}
};
Matrix x; struct Aho{
int ch[maxnode][sigma];
int f[maxnode];
bool val[maxnode];
int sz; void clear(){ sz = 1; ms(ch[0], 0); }
inline int idx(char ch){
if(ch == 'A') return 0;
if(ch == 'C') return 1;
if(ch == 'G') return 2;
return 3;
} void insert(const char *s){
int u = 0;
while(*s){
int c = idx(*s);
if(!ch[u][c]){
ms(ch[sz], 0);
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
++s;
}
val[u] = 1;
} void getFail(){
queue<int> q;
f[0] = 0;
for(int c = 0; c < sigma; ++c){
int u = ch[0][c];
if(u){ f[u] = 0; q.push(u); }
} while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c < sigma; ++c){
int u = ch[r][c];
if(!u){ ch[r][c] = ch[f[r]][c]; continue; }
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
val[u] |= val[f[u]];
}
}
} void solve(){
for(int i = 0; i < sz; ++i) if(!val[i]){
for(int j = 0; j < sigma; ++j){
int u = ch[i][j];
if(!val[u]) ++x.a[i][u];
}
}
x.n = sz;
}
}; Aho aho;
char s[maxnode]; Matrix fast_pow(Matrix x, int n){
Matrix res; res.cl;
res.n = x.n;
for(int i = 0; i < res.n; ++i) res.a[i][i] = 1;
while(n){
if(n&1) res = res * x;
n >>= 1;
x = x * x;
}
return res;
} int main(){
while(scanf("%d %d", &n, &m) == 2){
aho.cl;
for(int i = 0; i < n; ++i){
scanf("%s", s);
aho.insert(s);
}
aho.getFail();
x.cl;
aho.solve();
Matrix res = fast_pow(x, m);
int ans = 0;
for(int i = 0; i < res.n; ++i)
ans = (ans + res.a[0][i]) % mod;
printf("%d\n", ans);
}
return 0;
}