【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 ...
随机推荐
- IOS-UIProgressView的简单介绍
IOS-UIProgressView的简单介绍 转载:http://blog.sina.com.cn/s/blog_9c2363ad0101e1jy.html // UIProgressView的使用 ...
- GNU GRUB version 0.97 (630K lower /2053824K upper memory)
昨天把老板的IBM X61笔记本拿过来多系统,结果本以为很容易,直接ghost,结果悲剧发生啦,开机之后提示GNU GRUB version 0.97 (630K lower /2053824K up ...
- css考核点整理(八)-在什么情况下通过img引入图片,什么情况用背景图引入?背景属性有哪些
在什么情况下通过img引入图片,什么情况用背景图引入?背景属性有哪些
- sqlserver 2008表分区操作
表分区操作步骤 1.设计表进行分区的方案,水平分区.垂直分区 a.水平切割将减少表的行数,这样可以将历史数据归档,减少表大小,提高访问速度. b.垂直切割将分为主表和从表方式,将主要的列字段存放在主表 ...
- Rouh set 入门知识1(基础定义篇)
粗糙集理论是继概率论.模糊集.证据论后又一处理不完整性和不确定性的数学工具,建立在分类机制的基础上.无需提供问题所处理的数据集合之外的任何先验信息条件.并且能有效分析不精确.不一致.不完整等各种不完备 ...
- @PostConstruct与@PreDestroy
从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion):@PostConstruct和@PreDestroy.这两个注解被用来修饰一个非静态的vo ...
- u盘复制提示文件过大
应该有很多个朋友也遇到过同样的问题,就是我们的u盘的明明可用的空间还有很多,甚至一个空的16g的u盘,但从window等操作系统向u盘拷贝文件的时候,却不能容下诸如iso4g的镜像文件,难道是生产u盘 ...
- get 和 post的使用.
Two commonly used methods for a request-response between a client and server are: GET and POST. GET ...
- youphp学习整理
<?php //后台公共模块 // _list 数据显示 // add 添加/编辑 视图 // insert 添加处理函数 // edit 添加/编辑 视图 // update 更新处理函数 / ...
- Java环境的安装与配置
Java环境的安装与配置 环境:Java8,win10 推荐oracle官网oracle官网https://www.oracle.com/index.html下载JDK进行安装 选择自己需要的版本下载 ...