POJ-2778
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12726 | Accepted: 4862 |
Description
Suppose that DNA sequences of a species is a sequence that consist of A, C, T and G,and the length of sequences is a given integer n.
Input
Next m lines each line contain a DNA genetic disease segment, and length of these segments is not larger than 10.
Output
Sample Input
- 4 3
- AT
- AC
- AG
- AA
Sample Output
- 36
Source
推荐一个网址 :Maxtrix67
- /**
- 题意:给出n个字符串,问长度为m的字符串中有多少没有包含病毒
- 做法:AC自动机 + 矩阵快速幂
- Maxtrix 67 给出的定理8
- 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值
- 把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。 令C=A*A,
- 那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。
- 类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,
- 我们只需要二分求出A^k即可。
- **/
- #include <iostream>
- #include <cmath>
- #include <stdio.h>
- #include <algorithm>
- #include <string.h>
- #include <queue>
- #include <map>
- #define MM 10
- #define mod 100000
- using namespace std;
- struct Matrix
- {
- unsigned long long mat[][];
- int n;
- Matrix(){}
- Matrix(int _n)
- {
- n = _n;
- for(int i=;i<n;i++)
- {
- for(int j=;j<n;j++)
- {
- mat[i][j] = ;
- }
- }
- }
- Matrix operator *(const Matrix &b) const
- {
- Matrix res = Matrix(n);
- for(int i=;i<n;i++)
- {
- for(int j=;j<n;j++)
- {
- res.mat[i][j] = ;
- for(int k=;k<n;k++)
- {
- res.mat[i][j] += mat[i][k] * b.mat[k][j];
- }
- res.mat[i][j] %= mod;
- }
- }
- return res;
- }
- };
- unsigned long long quick_pow(unsigned long long a,int n)
- {
- unsigned long long res = ;
- unsigned long long tmp = a;
- while(n)
- {
- if(n&) res *= tmp;
- tmp *= tmp;
- n >>= ;
- }
- return res;
- }
- Matrix quick_pow(Matrix a,int n)
- {
- Matrix res = Matrix(a.n);
- for(int i=;i<a.n;i++)
- {
- res.mat[i][i] = ;
- }
- Matrix tmp = a;
- while(n)
- {
- if(n&) res =res * tmp;
- tmp = tmp * tmp;
- n >>= ;
- }
- return res;
- }
- struct Tire
- {
- int next[][MM],fail[];
- bool end[];
- int L,root;
- map<char,int>id;
- int newnode()
- {
- for(int i=;i<MM;i++)
- {
- next[L][i] = -;
- }
- end[L++] = ;
- return L-;
- }
- void init()
- {
- L = ;
- root = newnode();
- id['A'] = ;
- id['T'] = ;
- id['C'] = ;
- id['G'] = ;
- }
- void insert(char buf[])
- {
- int now = root;
- int len = strlen(buf);
- for(int i=;i<len;i++)
- {
- if(next[now][id[buf[i]]] == -)
- next[now][id[buf[i]]] = newnode();
- now = next[now][id[buf[i]]];
- }
- end[now] = true;
- }
- void build()
- {
- queue<int>que;
- int now = root;
- fail[root] = root;
- for(int i=;i<;i++)
- {
- if(next[now][i] == -)
- next[now][i] = root;
- else
- {
- fail[next[now][i]] = root;
- que.push(next[now][i]);
- }
- }
- while(!que.empty())
- {
- now = que.front();
- que.pop();
- if(end[fail[now]]) end[now] = true;
- for(int i=;i<;i++)
- {
- if(next[now][i] == -)
- next[now][i] = next[fail[now]][i];
- else
- {
- fail[next[now][i]] = next[fail[now]][i];
- que.push(next[now][i]);
- }
- }
- }
- }
- Matrix getMatrix()
- {
- Matrix res = Matrix(L+);
- for(int i=;i<L;i++)
- {
- for(int j=;j<;j++)
- {
- if(end[next[i][j]] == false && !end[i])
- res.mat[i][next[i][j]] ++;
- }
- }
- return res;
- }
- };
- char buf[];
- Tire ac;
- int main()
- {
- // freopen("in.txt","r",stdin);
- int n,m;
- while(~scanf("%d %d",&n,&m))
- {
- ac.init();
- for(int i=;i<n;i++)
- {
- scanf("%s",buf);
- ac.insert(buf);
- }
- ac.build();
- Matrix a = ac.getMatrix();
- a = quick_pow(a,m);
- unsigned long long res = ;
- for(int i=;i<a.n;i++)
- {
- res += a.mat[][i];
- }
- cout<<res%mod<<endl;
- }
- return ;
- }
POJ-2778的更多相关文章
- poj 2778 AC自己主动机 + 矩阵高速幂
// poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...
- POJ 2778 (AC自动机+矩阵乘法)
POJ 2778 DNA Sequence Problem : 给m个只含有(A,G,C,T)的模式串(m <= 10, len <=10), 询问所有长度为n的只含有(A,G,C,T)的 ...
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
- POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)
题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...
- POJ 2778 AC自己主动机+矩阵幂 不错的题
http://poj.org/problem?id=2778 有空再又一次做下,对状态图的理解非常重要 题解: http://blog.csdn.net/morgan_xww/article/deta ...
- POJ 2778 DNA Sequence(AC自动机+矩阵)
[题目链接] http://poj.org/problem?id=2778 [题目大意] 给出一些字符串,求不包含这些字符串的长度为n的字符串的数量 [题解] 我们将所有串插入自动机计算match,对 ...
- POJ 2778:DNA Sequence(AC自动机构造矩阵)
http://poj.org/problem?id=2778 题意:有m个病毒DNA,问构造一个长度为n的不带病毒DNA的字符串可以有多少种. 思路:看到这题有点懵,想了挺久题解的思路. 使用AC自动 ...
- POJ 2778 DNA Sequence(AC自动机+矩阵加速)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9899 Accepted: 3717 Desc ...
- POJ 2778 DNA sequence
QAQ 做完禁忌 又做过文本生成器 这道题就是个水题啦 首先转移方程还是文本生成器的转移方程 但是注意到L很大,但是节点数很小 转移都是固定的,所以我们可以用AC自动机来构造转移矩阵 之后进行矩阵乘法 ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
随机推荐
- 关于wesocket大文件通讯的切片实现方法
关于websocket的实现网上很多资料这里就不详说,这里大概讲我在websocket传输大文件的时的方法,websocket传输单个文件最大不能超过7kg,否则前段自动断掉,当我们用来语音通讯时,通 ...
- 15ecjtu校赛1006 (dfs容斥)
Problem Description 在平面上有一个n*n的网格,即有n条平行于x轴的直线和n条平行于y轴的直线,形 成了n*n个交点(a,b)(1<=a<=n,1<=b<= ...
- D-query SPOJ - DQUERY(莫队)统计不同数的数量
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- eclipse的最新版本luna的中建立svn和maven
http://blog.csdn.net/notillusion/article/details/40950185
- expect使用小结
因为工作关系,需要经常从线上机器上拉取数据,于是想着能否写个脚本,自动完成这个任务呢? 我一般使用scp在机器间传输文件,然而每次scp都需要输入密码,自动化脚本怎么解决这个问题呢?于是expect这 ...
- CodeVS 1017 DP
1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描 ...
- SNS应用好友动态Feed模块设计
转载自:http://libo93122.blog.163.com/blog/static/122189382012112145728902/ 备注:找不到原作者了. 现在大部分SNS网站都有一个功能 ...
- MySQL新建用户,授权
登录MySQL mysql -u root -p 添加新用户 允许本地 IP 访问 localhost, 127.0.0.1 create user 'test'@'localhost' identi ...
- LightOJ 1085 - All Possible Increasing Subsequences 树状数组+离散
http://www.lightoj.com/volume_showproblem.php?problem=1085 题意:求一个序列的递增子序列个数. 思路:找规律可以发现,某个数作为末尾数的种类数 ...
- Php扩展--seasLog日志扩展安装
安装/配置 编译安装 wge thttp://pecl.php.net/get/SeasLog-1.4.4.tgz tar -zxvfSeasLog-1.4.4.tgz cd SeasLog-1.4. ...