题意:给定一些串,然后让你构造出一个长度为 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. HTML5 Geolocation用来定位用户的位置。

    HTML5 Geolocation用来定位用户的位置. 定位用户的位置 HTMl5 Geolocation API用来得到用户的地理位置. 由于这个可能和个人隐私相关.除非用户同意否则不能使用. 浏览 ...

  2. 在 Windows 下安装 Oracle 11g XE (Express Edition)

    Oracle 11g XE 是 Oracle 数据库的免费版本,支持标准版的大部分功能,11g XE 提供 Windows 和 Linux 版本. 做为免费的 Oracle 数据库版本,XE 的限制是 ...

  3. 【转】ubuntu中没有/etc/inittab文件探究

    原文网址:http://blog.csdn.net/gavinr/article/details/6584582 linux 启动时第一个进程是/sbin/init,其主要功能就是软件执行环境,包括系 ...

  4. 【解决Jira】Chrome提示Java插件因过期而遭到阻止(JIRA上传截屏截图)

    最近经常被这个问题所困扰:用Chrome访问JIRA上传截屏截图时,地址栏下面弹出通知,提示JAVA插件已过期.但是由于公司要求统一开发环境和设置,不能更新到最新版,就像这样: 结果网页上的Java就 ...

  5. 显示等待WebDriverWait

    显示等待:WebDriverWait 等待页面加载完成,找到某个条件发生后再继续执行后续代码,如果超过设置时间检测不到则抛出异常 WebDriverWait(driver, timeout, poll ...

  6. 移动自动化测试:Android Studio 、Appium、夜神模拟器

    环境是Window 10 64位 第一章:安装Appium Appium和node.js需要一起安装,他们的依赖关系暂不深究. 1. node.js傻瓜式安装 官网地址:https://nodejs. ...

  7. 在 Ubuntu 16.04 LTS 上 离线安装 Docker / Docker-compose

    前情提要 今天上班后,突然接到现场的工程师的电话: XXX的现场环境组的的局域网,上不了互联网.bla bla bla..... 如果需要安装其他软件的话,只能是自己带过去安装... 听完现场工程师的 ...

  8. mysql迁移之巨大数据量快速迁移方案

    mysql迁移之巨大数据量快速迁移方案-增量备份及恢复 --chenjianwen 一.前言: 当mysql库的大小达到几十个G或者上百G,迁移起来是一件非常费事的事情,业务中断,导出导入耗费大量的时 ...

  9. centos7.3给搭建SVN服务器

    centos7.3给搭建SVN服务器 1 安装svnserver yum install subversion 2 查看版本 svnserve --version 3 创建版本库 3.1 运行以下命令 ...

  10. 一次使用 Redis 优化查询性能的实践

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,一次使用 Redis 优化查询性能的实践 应用背景 有一个应用需要上传一组ID到 ...