各种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. QT QXmlStreamWriter用法小结

    一 API介绍 writeStartDocument():写文档头,作用类似于创建一个xml文档,并在文档开头部分写入版本信息和编码信息,一般为: <?xml version="1.0 ...

  2. UNIX环境高级编程第二版代码笔记

    1. 第一个程序 gcc 1.1.c  /tmp/ccbnJqcB.o: In function `main': 1.1.c:(.text+0x17): undefined reference to ...

  3. ASP.NET MVC 第三回 Controller与View

    这节我们让ASP.NET MVC真正的跑起来 一.新建Controller 首先我们自己新建一个新的Controller在Controllers上点右键,添加,Controller选项   之后出现一 ...

  4. oo面向对象原则

    1.单一职责原则 一个类,最好只做一件事,只有一个引起他变化的原因否则就应该考虑重构. 2.开放封闭原则 软件实体应该是可扩展的,而不是可修改的.也就是说对扩展开放,对修改封闭.主要体现在两个方面: ...

  5. CSS Hack (各个浏览器兼容的问题)

    写css样式的时候,恐怕最头疼的就是各个浏览器下的兼容性问题,即css hack,明明感觉应该是对的,但是就是出不来效果,我根据平时所接触的,总结一下关于兼容 性的技巧,希望可以对大家有所帮助…… C ...

  6. PHP 开启报错机制

    屏蔽PHP错误提示 方法一:在有可能出错的函数前加@,然后or die("") 如: @mysql_connect(...) or die("Database Conne ...

  7. (转)从内存管 理、内存泄漏、内存回收探讨C++内存管理

    http://www.cr173.com/html/18898_all.html 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟 ...

  8. java web(jsp)-The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    在静态项目上新建 jsp文件的时候,报错:The superclass "javax.servlet.http.HttpServlet" was not found on the ...

  9. asp.net 实现对xml文件的 读取,添加,删除,修改

    用于修改站内xml文件 已知有一个XML文件(bookstore.xml)如下:<?xml version="1.0" encoding="gb2312" ...

  10. PHP算法 《树形结构》 之 伸展树(1) - 基本概念

    伸展树的介绍 1.出处:http://dongxicheng.org/structure/splay-tree/ A. 概述 二叉查找树(Binary Search Tree,也叫二叉排序树,即Bin ...