各种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的更多相关文章

  1. 【POJ】2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目 传送门:QWQ 分析 对着Trie图搞快速幂. 为什么这样是对的呢? 详见:http://www.matrix67.com/blog/archives/276 有些地方还不是很理解......为 ...

  2. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

  3. 【POJ】1141 Brackets Sequence

    经典DP问题,注意输入不要使用while(xxx != EOF),否则WA,测试数据只有一组.同样的测试数据可能有多种答案.但最小长度唯一.一定不能用while,切记. #include <io ...

  4. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  5. 【LeetCode】Repeated DNA Sequences 解题报告

    [题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  6. 【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, ...

  7. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  8. 【CF486E】LIS of Sequence题解

    [CF486E]LIS of Sequence题解 题目链接 题意: 给你一个长度为n的序列a1,a2,...,an,你需要把这n个元素分成三类:1,2,3: 1:所有的最长上升子序列都不包含这个元素 ...

  9. 【BZOJ4355】Play with sequence 线段树

    [BZOJ4355]Play with sequence Description 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a ...

随机推荐

  1. iOS UIScrollView 你可能不知道的奇技淫巧

    iOS 的 UIScrollView 可以说是十分强大,巧妙地运用它可以得到一些意想不到的效果.本文将举几个 ScrollView 不常见运用的例子. 自带信息应用 这个界面既可以上下卷动,也可以左右 ...

  2. MySQL(10):实体、实体表和外键(foreign key)

    1.实体        数据库管理系统中的各种用于数据管理方便而设定的各种数据管理对象,如:数据库表.视图.存储过程等都是数据库实体.广义上讲,这些对象中所存储的数据也是数据库实体.因为它们也是确切存 ...

  3. PHP编译错误Don't know how to define struct flock on this system, set --enable-opcache=no

    编辑 /etc/ld.so.conf 加入 /usr/local/lib 再执行 ldconfig

  4. LaTeX 中插入数学公式

    一.常用的数学符号 1.小写希腊字母 \alpha \nu \beta \xi \gamma o \delta \pi \epsilon \rho \zeta \sigma \eta \tau \th ...

  5. 国际化 native2ascii用法

    cmd下输入: native2ascii -encoding GBK(需要编译成哪种语言) (中文文件路劲) (英文文件路劲) 其他固定 例如 native2ascii -encoding GBK C ...

  6. 解决vim不能使用方向键和退格键问题

    1.使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母,或者退格键却变成方向键的功能 只要重装一下vi的依赖包即可完美解决vi编辑器方向键变字母的问题.rpm -e vim-enhance ...

  7. Oracle AWR报告指标全解析-11011552

    1-5 Top 5 Timed EventsWaits : 该等待事件发生的次数, 对于DB CPU此项不可用Times : 该等待事件消耗的总计时间,单位为秒, 对于DB CPU 而言是前台进程所消 ...

  8. ip接口调用

    <?php header("Content-type: text/html; charset=utf-8"); function getIP(){ if (isset($_S ...

  9. Linux通配符

    * 任意字符 ?任意单个字符 [] 匹配指定 字符范围内的字符 [^] 指定范围之外的单个字符 常规字符集合 [a-z] a到z的所有小写字母 [A-Z] a到z的所有大写字母 [0-9] 0到9的所 ...

  10. 【BZOJ1146】【树链剖分+平衡树】网络管理Network

    Description M 公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通 信网络.该网络的结构由N个 ...