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 ...
随机推荐
- [Leetcode] maximun subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- git使用笔记(十二)stash
By francis_hao Oct 29,2017 git stash 保存当前工作目录的修改 概要 git stash list [<options>]git stash s ...
- 名人问题/名流问题/Celebrity
问题描述:名人问题一个名人就是指这样一个人:所有其他人都认识他,并且他不认识任何其他人.现在有一个N个人的集合,以及他们之间的认识关系.求一个算法找出其中的名人(如果有的话)或者判断出没有名人(如果没 ...
- [技巧篇]17.那些年一直再逃避的问题,还债Web阶段!
四海行唐的一阶段和二阶段的时候,再使用数据的时候总是使用List<Map<String,Object>>的东西,但是胖先生一直不怎么喜欢! 所以我再凌云17的Web阶段的时候, ...
- windows版本redis下载安装
官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 在官方下载网页中有一个window版本的说明,说redis官方没有wi ...
- 读取txt内容放入set中
package pingbi; /** * 将txt文本读入导入到set中 * 问题: * 第一个地方有会多一个 ?--解决问题很简单,但不知道问题的原因 */ import java.io.Buff ...
- ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer
21:09:22.221 [MessageBroker-3] INFO c.t.s.s.impl.StockNewsServiceImpl - [2017-12-16 21:09:22] execut ...
- 【Codeforces542E】Playing on Graph [Bfs][Dfs]
Playing on Graph Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 5 4 ...
- bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形——极角排序
Description 在一只大灰狼偷偷潜入Farmer Don的牛群被群牛发现后,贝西现在不得不履行着她站岗的职责.从她的守卫塔向下瞭望简直就是一件烦透了的事情.她决定做一些开发智力的小练习,防止她 ...
- JS对象操作
一.String常用操作 1.截取 substr(start,length) //返回从指定位置开始的指定长度的字符串. substring(start,end) //返回两个指定的位置之间的字符串. ...