题意:给定一些串,然后让你构造出一个长度为 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;
}

  

POJ 2778 DNA Sequence (AC自动机+DP+矩阵)的更多相关文章

  1. poj 2778 DNA Sequence AC自动机DP 矩阵优化

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  2. POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...

  3. poj 2778 DNA Sequence ac自动机+矩阵快速幂

    链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...

  4. POJ 2778 DNA Sequence (AC自动机,矩阵乘法)

    题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...

  5. poj 2778 DNA Sequence AC自动机

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  6. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  7. POJ 3691 DNA repair(AC自动机+DP)

    题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...

  8. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

  9. POJ 2778 DNA Sequence ( Trie图、矩阵快速幂 )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析: 我们先分析Tire 图的结构 : Trie图是在AC自动机的原型上增添边使得状态可以快速转移,标记危 ...

随机推荐

  1. 怎样创造財富?硅谷创业之父 Paul Graham 《黑客与画家》思维导图

    先送上亚马逊传送门:<黑客与画家>:硅谷创业之父 Paul Graham 文集 再送上一个思维导图: 下载大图:http://caifujianghu.com/article/ruhe-c ...

  2. cocos2dx ui显示机制

    实验1 1,a.addChild(b); a的宽高没变,还是自己的宽高. 层级添加  不会改变原层大小. 2.node.addChild(sprite);node的宽和高也没变 感觉2dx的显示不是树 ...

  3. Java里的集合--主要区别

    Collection 集合接口,指的是 java.util.Collection接口,是 Set.List 和 Queue 接口的超类接口. List: List是关注事物索引的列表. List中可以 ...

  4. 三大运营商2G/3G/4G频率分配和网络制式

    经过二十多年长期的发展,我国的通信业逐渐形成了2G/3G/4G并存的局面,手机通讯信号传输都是通过一定频率传输的,而三大运营商所拥有的频率和网络制式不尽相同,这就造成同一部手机在三大运营商之间可能不通 ...

  5. SSH: sshd dead but subsys locked

    问题: 查看SSH的状态时,提示错误如下: /etc/init.d/sshd status error: sshd dead but subsys locked 解决方法: sshd -d rm -r ...

  6. 461. Hamming Distance + 477. Total Hamming Distance

    ▶ 与 Hamming  距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...

  7. JQ与JS等价代码

    选择器 //jquery var els = $(".el"); //原生方法 var els = document.querySelectorAll(".el" ...

  8. _kbhit() for linux

    传送门:http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html #include <stdio.h> #in ...

  9. 迷你MVVM框架 avalonjs 1.3.5发布

    本版本主要是修复内存泄漏问题,让其在移动端更好的运作. 修正visible BUG 详见这里 修正$fire方法里的正则错误 详见这里 修正ms-attr BUG,在IE9-11,直接用element ...

  10. springmvc 类型转换器 数据回显及提示信息

    处理器的写法: 类型转换器的写法: 类型转换器在springmvc.xml中的配置如下: index.jsp的写法: