【POJ】2278 DNA Sequence
各种wa后,各种TLE。注意若AC非法,则ACT等一定非法。而且尽量少MOD。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXN 105
#define NXTN 4 char str[]; typedef struct Matrix {
int n;
__int64 map[MAXN][MAXN];
Matrix() {}
Matrix(int nn) {
n = nn;
for (int i=; i<nn; ++i)
for (int j=; j<nn; ++j)
map[i][j] = ;
}
} Matrix; int getval(char ch) {
switch (ch) {
case 'A': return ;
case 'C': return ;
case 'T': return ;
case 'G': return ;
default: return ;
}
} struct AC {
int next[MAXN][NXTN], fail[MAXN], v[MAXN];
int n, root;
void init() {
n = ;
root = newNode();
}
int newNode() {
fail[n] = ;
v[n] = ;
memset(next[n], -, sizeof(next[n]));
return n++;
}
void create(char s[]) {
int i = , id;
int cur = root; while (s[i]) {
id = getval(s[i]);
++i;
if (next[cur][id] == -)
next[cur][id] = newNode();
cur = next[cur][id];
}
v[cur] = ;
}
void build() {
int i, cur;
queue<int> Q; fail[root] = root;
for (i=; i<NXTN; ++i) {
if (next[root][i] == -)
next[root][i] = root;
else {
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
} while (!Q.empty()) {
cur = Q.front();
Q.pop();
if (v[fail[cur]]) // important
v[cur] = ;
for (i=; i<NXTN; ++i) {
if (next[cur][i] == -)
next[cur][i] = next[fail[cur]][i];
else {
fail[next[cur][i]] = next[fail[cur]][i];
Q.push(next[cur][i]);
}
}
}
}
Matrix query() {
int i, j; Matrix mat(n); for (i=; i<n; ++i) {
for (j=; j<NXTN; ++j) {
if (v[next[i][j]] == )
++mat.map[i][next[i][j]];
}
}
return mat;
}
}; AC ac; Matrix mmul(Matrix a, Matrix b) {
int i, j, k, n = a.n;
Matrix ret(n); for (i=; i<n; ++i)
for (j=; j<n; ++j)
for (k=; k<n; ++k) {
ret.map[i][j] += a.map[i][k]*b.map[k][j];
ret.map[i][j] %= ;
}
return ret;
} void mpow(Matrix a, int n) {
Matrix ret(a.n);
int i;
__int64 ans; for (i=; i<ret.n; ++i)
ret.map[i][i] = ; while (n) {
if (n & )
ret = mmul(ret, a);
a = mmul(a, a);
n >>= ;
} ans = ;
for (i=; i<ret.n; ++i) {
//printf("i:%I64d\n", ret.map[0][i]);
ans += ret.map[][i];
ans %= ;
}
printf("%I64d\n", ans);
} int main() {
int m, n;
int i; while (scanf("%d %d", &m, &n) != EOF) {
ac.init();
for (i=; i<m; ++i) {
scanf("%s", str);
ac.create(str);
}
ac.build();
Matrix mat = ac.query();
//printf("mat.n=%d\n", mat.n);
mpow(mat, n);
} return ;
}
【POJ】2278 DNA Sequence的更多相关文章
- 【POJ】2778 DNA Sequence(AC自动机+矩阵快速幂)
题目 传送门:QWQ 分析 对着Trie图搞快速幂. 为什么这样是对的呢? 详见:http://www.matrix67.com/blog/archives/276 有些地方还不是很理解......为 ...
- 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function
http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...
- 【POJ】1141 Brackets Sequence
经典DP问题,注意输入不要使用while(xxx != EOF),否则WA,测试数据只有一组.同样的测试数据可能有多种答案.但最小长度唯一.一定不能用while,切记. #include <io ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【CF486E】LIS of Sequence题解
[CF486E]LIS of Sequence题解 题目链接 题意: 给你一个长度为n的序列a1,a2,...,an,你需要把这n个元素分成三类:1,2,3: 1:所有的最长上升子序列都不包含这个元素 ...
- 【BZOJ4355】Play with sequence 线段树
[BZOJ4355]Play with sequence Description 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a ...
随机推荐
- mac tips
1. Mac Terminal color for different types 在 ~ 先建立一个文件 ~/.bash_profile 加入下面的两行:export CLICOLOR=1expo ...
- ckeditor 敏感词标记显示处理方法
直接在原型添加方法: (function () { /* * 取消所有高亮 */ CKEDITOR.editor.prototype.CancleSensitiveWordsHighlight = f ...
- 适配器控件-Adapter
适配器对象派生自Android.widget.Adapter,它的作用包括:构造列表项控件,并将数据项绑定到列表项控件中. 常见的适配器有:数组适配器 ArrayAdapter,数据库适配器 Curs ...
- hadoop集群环境搭建准备工作
一定要注意hadoop和linux系统的位数一定要相同,就是说如果hadoop是32位的,linux系统也一定要安装32位的. 准备工作: 1 首先在VMware中建立6台虚拟机(配置默认即可).这是 ...
- SQL Server中的死锁
简介 死锁的本质是一种僵持状态,是多个主体对于资源的争用而导致的.理解死锁首先需要对死锁所涉及的相关观念有一个理解. 一些基础知识 要理解SQL Server中的死锁,更好的方式是通过类比从更大的面理 ...
- 11g 创建asm磁盘组
[root@Oracle11g ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes255 heads, 63 sectors/track, 2 ...
- asp.net发布和更新网站
我们一般使用ftp软件来更新网站,而更新之前的一个步骤就是发布项目.以下将讲解asp.net mvc如何发布网站. 打开项目 右键点击项目,选择“发布” 第一次发布前,需要配置一下发布配置文件:点击” ...
- Java线程(学习整理)--1--守护线程
1.什么是守护线程? 今天老师讲解我才知道有守护线程这回事!原来守护线程经常存在于我们的身边,比如:一个免费的网页游戏,里面都会或多或少有些插入性的广告!! 一般情况下,我们不会去点击这些广告的,但是 ...
- NOIP2012 借教室 Splay初探
终于把区间操作的Splay搞明白了…… Splay的大致框架是这样的: [代码中的Zig-Zig和Zig-Zag操作其实是可以优化的,实际只需要3次passDown和3次update] templat ...
- 安装freebsd9 出现 mountroot>怎么办
之前手欠把linux分区给删了想重装freebsd 重新进入的时候mbr提示grub信息 用PE把MBR删掉 之后再用freebsd光盘启动出现mountroot> 就用mountroot> ...