POJ 2778 (AC自动机+矩阵乘法)
POJ 2778 DNA Sequence
Problem : 给m个只含有(A,G,C,T)的模式串(m <= 10, len <=10), 询问所有长度为n的只含有(A,G,C,T)的串中有多少个不含有模式串的串。(n<=2000000000)
Solution :首先对所有模式串建立AC自动机。然后dp[i][j]表示长度为i,走到AC自动机的节点j这样的字符串满足条件的个数有多少,用AC自动机的边写出状态转移方程然后用矩阵快速幂加速运算。
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int N = 208;
const int mo = 100000;
int id[128];
struct Matrix
{
int n;
int a[N][N];
Matrix(int n_, int p)
{
n = n_;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
a[i][j] = 0;
if (i == j) a[i][j] = p;
}
}
friend Matrix operator *(Matrix A, Matrix B)
{
Matrix C(A.n, 0);
for (int i = 0; i < A.n; ++i)
for (int j = 0; j < A.n; ++j)
for (int k = 0; k < A.n; k++)
C.a[i][j] = (C.a[i][j] + 1ll * A.a[i][k] * B.a[k][j] % mo) % mo;
return C;
}
void print()
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j) cout << a[i][j] << " ";
cout << endl;
}
}
};
struct AC_Automan
{
int next[N][4];
int fail[N];
int cnt[N];
int root, tot;
int newnode()
{
for (int i = 0; i <= 3; ++i) next[tot][i] = -1;
fail[tot] = cnt[tot] = -1;
return tot++;
}
void clear()
{
tot = 0;
root = newnode();
}
void insert(const string &s)
{
int p = root;
for (int i = 0, len = s.length(); i < len; ++i)
{
if (next[p][id[s[i]]] == -1) next[p][id[s[i]]] = newnode();
p = next[p][id[s[i]]];
}
cnt[p] = 1;
}
void build()
{
queue <int> Q;
Q.push(root);
while (!Q.empty())
{
int p = Q.front(); Q.pop();
for (int i = 0; i < 4; ++i)
{
if (~next[p][i])
{
if (p == root) fail[next[p][i]] = root;
else fail[next[p][i]] = next[fail[p]][i];
Q.push(next[p][i]);
}
else
{
if (p == root) next[p][i] = root;
else next[p][i] = next[fail[p]][i];
}
}
}
}
Matrix power(Matrix A, int y)
{
Matrix B(tot, 1);
while (y)
{
if (y & 1) B = B * A;
A = A * A;
y >>= 1;
}
return B;
}
void solve(int num)
{
Matrix A(tot, 0);
for (int i = 0; i <= tot; ++i)
{
for (int j = 0; j < 4; ++j)
{
int flag = 1;
for (int temp = next[i][j]; temp != root; temp = fail[temp])
{
if (~cnt[temp]) flag = 0;
}
A.a[i][next[i][j]] += flag;
}
}
A = power(A, num);
int ans = 0;
for (int i = 0; i < tot; ++i) ans = (ans + A.a[0][i]) % mo;
cout << ans << endl;
}
}ac;
int main()
{
cin.sync_with_stdio(0);
id['A'] = 0; id['G'] = 1; id['C'] = 2; id['T'] = 3;
int m, n;
while (cin >> m >> n)
{
ac.clear();
for (int i = 1; i <= m; ++i)
{
string s; cin >> s;
ac.insert(s);
}
ac.build();
ac.solve(n);
}
}
POJ 2778 (AC自动机+矩阵乘法)的更多相关文章
- DNA Sequence POJ - 2778 AC 自动机 矩阵乘法
定义重载运算的时候一定要将矩阵初始化,因为这个调了一上午...... Code: #include<cstdio> #include<algorithm> #include&l ...
- poj 2778 AC自动机+矩阵快速幂
题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...
- DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂
It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...
- 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法
题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...
- POJ 2778 DNA Sequence (AC自动机,矩阵乘法)
题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...
- DNA Sequence - POJ 2778(AC自动机+矩阵乘法)
题目大意:DNA序列是有 ATGC 组成的,现在知道一些动物的遗传片段有害的,那么如果给出这些有害的片段,能否求出来所有长度为 N 的基因中有多少是不包含这些有害片段的. 分析:也是断断续续做了一 ...
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...
- 【POJ2778】AC自动机+矩阵乘法
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14758 Accepted: 5716 Descrip ...
- bzoj 2553: [BeiJing2011]禁忌 AC自动机+矩阵乘法
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2553 题解: 利用AC自动机的dp求出所有的转移 然后将所有的转移储存到矩阵中,进行矩阵 ...
随机推荐
- solr 6.0 没有schema.xml未自动创建schema文件
solr 6.0 没有schema.xml未自动创建schema文件 摘要:在之前的Solr版本中(Solr5之前),在创建core的时候,Solr会自动创建好schema.xml,但是在之后的版本中 ...
- [转]无废话SharePoint入门教程一[SharePoint概述]
本文转自:http://www.cnblogs.com/iamlilinfeng/p/3026332.html 一.前言 听说SharePoint也有一段时间了,可一直处在门外.最近被调到ShareP ...
- AJPFX:学习JAVA程序员两个必会的冒泡和选择排序
* 数组排序(冒泡排序)* * 冒泡排序: 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处* * 选择排序 : 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现 ...
- mac重启iterm后不会自动加载.bash_profile
我用的zsh,由于平时设置的环境变量都是在.bash_profile文件中,每次重启iterm后,都需要重启手动加载.bash_profile文件,很麻烦. 设置自动加载.bash_profile的方 ...
- 提交应用 Windows Phone的应用程序认证要求
本文介绍了 Windows Phone 应用程序或游戏要通过认证并在 Windows Phone Marketplace 中发布而必须满足的策略和技术要求. 1.0 计划概述 设计认证过程的一个核心原 ...
- SQL将查询出来的多列的值拼接成一个字符串
-- 单列拼接,先查出一行,再加上逗号,接着拼接 查出的下一行 SELECT GROUP_CONCAT(user_id) FROM user; -- result 160,160,160,196 -- ...
- springMVC返回Base64位编码图片验证码
import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...
- emil 的使用
摘抄自别人 RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔. 邮件头包含的内容有: from字段 --用于指明发 ...
- Java加密简介
加密算法: 1.对称加密 DES AES 2.非对称加密 RSA 3.散列函数算法加密 (单项加密)::MD5.SHA.Mac 4.数字签名算法:RSA.DSA 其中,前三种主要完成数据的加解密: ...
- _bbox_pred函数
fast中的_bbox_pred函数和faster中的bbox_transform_inv是一样的,是将框进行4个坐标变换得到新的框坐标.fast中是将selective search生成的框坐标进行 ...