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求出所有的转移 然后将所有的转移储存到矩阵中,进行矩阵 ...
随机推荐
- C. Two strings 二分 + 预处理
http://codeforces.com/contest/762/problem/C 第一个串str[],第二个sub[] 预处理出prefix[i]表示sub的前i位和str[]的最长lcs去到s ...
- 初学Ajax
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...
- mybatis的mapper.xml文件细节
- 锐动SDK应用于行车记录仪
方案架构 手机端直播与录播功能忠实记录旅途中各种突发事件,还原事实真相,与家人和朋友分享沿途美景,一同感受美妙之旅. 强大的视频编辑功能,像编辑图片一样给视频添加各种滤镜,配音,配乐,标题文字等特效. ...
- (2) Tomcat启动Jenkins
Tomcat启动Jenkins 1. 下载Tomact,解压缩到指定目录. 2. 下载Jenkins.war文件,方到Tomact的WebApps下面即可. 3. 修改Tomcat的HTTP端口和默认 ...
- [Windows Server 2008] PHP安装Memcached
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:Windows ...
- android 开源
http://blog.csdn.net/xiaoxiao_job/article/details/45196119?ref=myread MPAndroidChart https://github. ...
- C/C++ 各进制赋值、int/char转换、sscanf/sprintf、位操作运算
一.各进制赋值 1.十六进制赋值 int i=0x12AD; int i=0X12AD; int i=0x12Ad; int i=0X12Ad; //以上都是十六进制,表示十进制173: 2.八进制赋 ...
- c++ 模板template
1.函数模板的声明 声明形式 template<typename 数据类型参数标识符> <返回类型><函数名>(参数表) { 函数体 } 注: templa ...
- AttributeError: 'dict' object has no attribute 'encode'
首先这是一个很简单的 运行时错误: 错误分析: AttributeError:属性错误,造成这种错误的原因可能有: 你尝试访问一个不存在的属性或方法.检查一下拼写!你可以使用内建函数 dir 来列出存 ...