题目链接:http://poj.org/problem?id=2778

题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列。(仅含A,T,C,G四个字符)

思路:Trie图的状态转移,用矩阵mat[i][j]来表示从结点i到j只走一步有几种走法,那么mat的n次幂就表示从结点i到j走n步有几种走法,题目要求解的就是从头节点走n步且不包含危险结点的走法。

mat = mat^n   ans = (mat[0][0] + mat[0][1] + ... + mat[0][num]) num为结点个数

code:

 #include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
const int KIND = ;
const int MAXN = ;
const int MOD = ;
typedef long long LL; struct Trie
{
int next[MAXN][KIND], fail[MAXN];
bool isExit[MAXN];
int root, L;
map<char, int> mp;
LL mat[MAXN][MAXN];
LL ret[MAXN][MAXN];
LL tmp[MAXN][MAXN];
int create()
{
for (int i = ; i < KIND; ++i)
next[L][i] = -;
isExit[L++] = false;
return L - ;
}
void init()
{
L = ;
root = create();
mp['A'] = ;
mp['C'] = ;
mp['G'] = ;
mp['T'] = ;
memset(mat, , sizeof(mat));
memset(ret, , sizeof(ret));
}
void insert(char str[])
{
int now = root;
int len = strlen(str);
for (int i = ; i < len; ++i)
{
if (- == next[now][mp[str[i]]])
next[now][mp[str[i]]] = create();
now = next[now][mp[str[i]]];
}
isExit[now] = true;
}
void build()
{
queue<int>Q;
for (int i = ; i < KIND; ++i)
{
if (- == next[root][i])
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while (!Q.empty())
{
int now = Q.front();
Q.pop();
if (isExit[fail[now]])
isExit[now] = true;
for (int i = ; i < KIND; ++i)
{
if (- == next[now][i])
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
void getMatrix()
{
for (int i = ; i < L; ++i)
{
for (int j = ; j < KIND; ++j)
{
if (!isExit[next[i][j]])
++mat[i][next[i][j]];
}
}
}
void matrixMul(LL mat1[MAXN][MAXN], LL mat2[MAXN][MAXN])
{
LL mat3[MAXN][MAXN];
for (int i = ; i < L; ++i)
{
for (int j = ; j < L; ++j)
{
mat3[i][j] = ;
for (int k = ; k < L; ++k)
mat3[i][j] = (mat3[i][j] + mat1[i][k] * mat2[k][j]) % MOD;
}
}
memcpy(mat1, mat3, sizeof(mat3));
}
void matrixQuickMod(LL n)
{
getMatrix();
for (int i = ; i < L; ++i)
{
ret[i][i] = ;
for (int j = ; j < L; ++j)
tmp[i][j] = mat[i][j];
}
while (n)
{
if (n & ) matrixMul(ret, tmp);
matrixMul(tmp, tmp);
n >>= ;
}
}
};
Trie ac;
char str[];
int main()
{
int m;
LL n;
while (scanf("%d %lld", &m, &n) != EOF)
{
ac.init();
for (int i = ; i < m; ++i)
{
scanf("%s", str);
ac.insert(str);
}
ac.build();
ac.matrixQuickMod(n);
int ans = ;
for (int i = ; i < ac.L; ++i)
ans = (ans + ac.ret[][i]) % MOD;
printf("%d\n", ans);
}
return ;
}

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

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

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

  2. POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂

    这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...

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

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

  4. [poj2778]DNA Sequence(AC自动机+矩阵快速幂)

    题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...

  5. poj2778 DNA Sequence(AC自动机+矩阵快速幂)

    Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's ve ...

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

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

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

    题意:给定一些串,然后让你构造出一个长度为 m 的串,并且不包含以上串,问你有多少个. 析:很明显,如果 m 小的话 ,直接可以用DP来解决,但是 m 太大了,我们可以认为是在AC自动机图中,根据离散 ...

  8. poj2778DNA Sequence (AC自动机+矩阵快速幂)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud DNA Sequence Time Limit: 1000MS   Memory ...

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

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

  10. poj 2778 DNA Sequence AC自动机

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

随机推荐

  1. iOS中Blocks的介绍

    1. 什么是Blocks Blocks是C语言的扩充功能.如果用一句话来概括就是:带有自动变量的匿名函数. 第一次看见Blocks的时候,感觉很类似C语言的函数指针,尤其是Block类型变量,更是有极 ...

  2. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  3. yum subversion puppet puppet-server

    yum -y install ruby ruby-libs ruby-shadow yum -y install puppet puppet-server facter yum -y install ...

  4. css新属性box-sizing应用

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Address already in use: JVM_Bind <null>:8080

    解决方法: 1重开eclipse,端口号被占用,或者杀掉进程

  6. matlab改变GUI和figure左上角图标的方法,并生成exe文件

    1. GUI左上角图标的更改,假设GUI的Tag为figure1,在其OpeningFcn里添加h = handles.figure1; %返回其句柄newIcon = javax.swing.Ima ...

  7. C++的标准模板库(STL)简介

    STL(Standard Template Library,标准模板库)是C++对泛型编程思想的实现,最早是惠普实验室开发的.在被引入C++之前该技术就已经存在了很长的一段时间.后来STL成为ANSI ...

  8. 关于SVM一篇比较全介绍的博文

    转自:http://blog.csdn.net/v_july_v/article/details/7624837 支持向量机通俗导论(理解SVM的三层境界) 前言 动笔写这个支持向量机(support ...

  9. 读书笔记 - 设计模式(Head First)

    设计模式让你和其他开发人员之间有共享的词汇,设计模式可以把你的思考架构的层次提高到模式层面,而不是停留在琐碎的对象上. 设计原则: 封装变化:找出应用中可能需要变化之处,把它们独立出来,不要和那些不需 ...

  10. 如何为你的美术妹子做Unity的小工具(三)

    绘制脚本组件监视面板的内容 我们写了一个脚本Test using UnityEngine; using System.Collections; using System.Collections.Gen ...