【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 ...
随机推荐
- oracle中的层级递归查询操作
oracle中的层级操作非常方便,在使用之后爱不释手,以前要实现该种数据查询操作,需要非常复杂的实现过程.在oracle中通过connect by可以实现前面的目的,通常情况下层级查询基本都能实现递归 ...
- python 学习笔记(一)
在Windows上安装Python 首先,从Python的官方网站www.python.org下载最新的2.7.9版本,地址是这个: http://www.python.org/ftp/python/ ...
- c# 操作.config中AppSettings配置节
ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...
- 在C语言中使用scanf语句时遇到的问题总结
在使用visual studio2013编写c语言代码时,遇到了这样的几个小问题,进行如下的总结. 1, 关于使用scanf语句报错的解决方案1 #include <stdio.h> in ...
- tableView尾部多处一部分空白高度
问题出现的原因:创建tableView时使用的style是UITableViewStylePlain 解决办法: 在创建tableView时,self.automaticallyAdjustsScro ...
- iOS 获取通讯录里边的电话号码AddressBook
1 首先导入库 <AddressBook/AddressBook.h> 2 然后在导入#import <AddressBook/AddressBook.h>文件 3 声明 ...
- 读懂IL代码(一)
以前刚开始学C#的时候,总有高手跟我说,去了解一下IL代码吧,看懂了你能更加清楚的知道你写出来的代码是如何运行互相调用的,可是那时候没去看,后来补的,其实感觉也不晚.刚开始看IL代码的时候,感觉非常吃 ...
- jQuery 删除元素
通过 jQuery,可以很容易地删除已有的 HTML 元素. 删除元素/内容 如需删除元素和内容,一般可使用以下两个 jQuery 方法: remove() - 删除被选元素(及其子元素) empty ...
- hadoop 分片与分块,map task和reduce task的理解
分块:Block HDFS存储系统中,引入了文件系统的分块概念(block),块是存储的最小单位,HDFS定义其大小为64MB.与单磁盘文件系统相似,存储在 HDFS上的文件均存储为多个块,不同的是, ...
- 1. mybatis批量插入数据
通过list <insert id="saveByList" useGeneratedKeys="true" parameterType="ja ...