题意:给一些字符串的集合S和整数n,求满足

  • 长度为n
  • 只含charset = {'A'、'T‘、'G'、'C'}包含的字符
  • 不包含S中任一字符串

的字符串的种类数。

思路:首先对S建立ac自动机,考虑向ac自动机中的每种状态后加charset中的字符,如果终态不为“接受状态”,也就是不与S中的任一字符串匹配,则将这次转移记为有效,方法数加1。这样可以建立状态之间的转移矩阵D,表示由一个状态接受1个字符后的方案数,D自乘n次,就得到了任一状态接受n个字符形成的不同字符串种类数,其中从“0”到“i”的方案都要加到答案里面。

另外,这题的模比较有意思,为100000,既不大也不小,或者说是个比较猥琐的数。对于100*100的矩阵,这样取模当然是吃不消的,方法是对每个点算完后一次性取模。时间由800ms→125ms。

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fillarray(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; #ifndef ONLINE_JUDGE
namespace Debug {
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
#endif // ONLINE_JUDGE template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double EPS = 1e-14; /* -------------------------------------------------------------------------------- */ const int maxn = ;
const int mod = ; int N; struct Matrix {
ll a[maxn][maxn]; Matrix() {
for (int i = ; i < N; i ++) {
for (int j = ; j < N; j ++) {
a[i][j] = ;
}
}
} static Matrix unit() {
Matrix ans;
for (int i = ; i < N; i ++) ans.a[i][i] = ;
return ans;
} Matrix &operator * (const Matrix &that) const {
static Matrix ans;
for (int i = ; i < N; i ++) {
for (int j = ; j < N; j ++) {
ans.a[i][j] = ;
for (int k = ; k < N; k ++) {
ans.a[i][j] += a[i][k] * that.a[k][j];
}
ans.a[i][j] %= mod;
}
}
return ans;
} static Matrix power(Matrix a, int n) {
Matrix ans = unit(), buf = a;
while (n) {
if (n & ) ans = ans * buf;
buf = buf * buf;
n >>= ;
}
return ans;
}
}; struct AC_automaton {
const static int SZ = ;
int f[maxn], last[maxn];
bool val[maxn];
int ch[maxn][SZ];
int sz; int idx(char ch) {
if (ch == 'A') return ;
if (ch == 'T') return ;
if (ch == 'G') return ;
if (ch == 'C') return ; } void init() {
sz = ;
memset(ch[], , sizeof(ch[]));
} void insert(char s[]) {
int u = ;
for (int i = ; s[i]; i ++) {
int c = idx(s[i]);
if (!ch[u][c]) {
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz ++;
}
u = ch[u][c];
}
val[u] = true;
} void build() {
queue<int> Q;
f[] = ;
for (int c = ; c < SZ; c ++) {
int u = ch[][c];
if (u) {
f[u] = ;
Q.push(u);
last[u] = ;
}
}
while (!Q.empty()) {
int r = Q.front(); Q.pop();
for (int c = ; c < SZ; c ++) {
int u = ch[r][c];
if (!u) {
ch[r][c] = ch[f[r]][c];
continue;
}
Q.push(u);
int v = f[r];
while (v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]]? f[u] : last[f[u]];
}
}
} void solve(int m) {
N = sz;
Matrix ans;
//Debug::print(sz);
for (int i = ; i < sz; i ++) {
for (int j = ; j < ; j ++) {
if (!val[ch[i][j]] && !last[ch[i][j]]) {
ans.a[i][ch[i][j]] ++;
}
}
}
ans = Matrix::power(ans, m);
int rst = ;
for (int i = ; i < sz; i ++) {
rst = (rst + ans.a[][i]) % mod;
}
printf("%d\n", rst);
} }; AC_automaton ac; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, m;
char s[];
while (cin >> n >> m) {
ac.init();
for (int i = ; i < n; i ++) {
scanf("%s", s);
ac.insert(s);
}
ac.build();
ac.solve(m);
}
return ;
}

[poj2778 DNA Sequence]AC自动机,矩阵快速幂的更多相关文章

  1. [poj2778]DNA Sequence(AC自动机+矩阵快速幂)

    题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...

  2. poj2778 DNA Sequence(AC自动机+矩阵快速幂)

    Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's ve ...

  3. poj 2778 DNA Sequence ac自动机+矩阵快速幂

    链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...

  4. poj2778DNA Sequence (AC自动机+矩阵快速幂)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud DNA Sequence Time Limit: 1000MS   Memory ...

  5. POJ2778 DNA Sequence(AC自动机 矩阵)

    先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...

  6. POJ2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目给m个病毒串,问不包含病毒串的长度n的DNA片段有几个. 感觉这题好神,看了好久的题解. 所有病毒串构造一个AC自动机,这个AC自动机可以看作一张有向图,图上的每个顶点就是Trie树上的结点,每个 ...

  7. POJ 2778 DNA Sequence (ac自动机+矩阵快速幂)

    DNA Sequence Description It's well known that DNA Sequence is a sequence only contains A, C, T and G ...

  8. DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂

    It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...

  9. POJ 2778 DNA Sequence(AC自动机 + 矩阵快速幂)题解

    题意:给出m个模式串,要求你构造长度为n(n <= 2000000000)的主串,主串不包含模式串,问这样的主串有几个 思路:因为要不包含模式串,显然又是ac自动机.因为n很大,所以用dp不太好 ...

  10. POJ2778(SummerTrainingDay10-B AC自动机+矩阵快速幂)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17160   Accepted: 6616 Des ...

随机推荐

  1. vue2.x学习笔记(十六)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12616543.html. 组件中的插槽 在2.6.0的版本中,vue为具名插槽和作用域插槽引入了一个新的统一的语法 ...

  2. linux常用命令--文件和目录

    cd /home 进入 '/ home' 目录' cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd 进入个人的主目录 cd ~user1 进入个人的主目录 cd - 返回上次所在的目 ...

  3. [PHP]PHP设计模式:单例模式

    单例模式(职责模式): 简单的说,一个对象(在学习设计模式之前,需要比较了解面向对象思想)只负责一个特定的任务: 单例类: 1.构造函数需要标记为private(访问控制:防止外部代码使用new操作符 ...

  4. C#反射(二)

    长时间没有回顾反射知识了,今天就讲解一下反射的一般第二个用法. 二.对方法,属性等的反射 首先需要写一个测试类,生成.exe或.dll文件. class Test {   public Test()/ ...

  5. Java的数组索引问题

    /* 数组操作的两个常见小问题: ArrayIndexOutOfBoundsException:数组索引越界异常 原因:你访问了不存在的索引. NullPointerException:空指针异常 原 ...

  6. 机器学习5- 对数几率回归+Python实现

    目录 1. 对数几率回归 1.1 求解 ω 和 b 2. 对数几率回归进行垃圾邮件分类 2.1 垃圾邮件分类 2.2 模型评估 混淆举证 精度 交叉验证精度 准确率召回率 F1 度量 ROC AUC ...

  7. 数值计算方法实验之Newton 多项式插值(MATLAB代码)

    一.实验目的 在己知f(x),x∈[a,b]的表达式,但函数值不便计算或不知f(x),x∈[a,b]而又需要给出其在[a,b]上的值时,按插值原则f(xi)=yi (i=0,1,……, n)求出简单函 ...

  8. 文件包含漏洞(pikachu)

    文件包含漏洞 在web后台开发中,程序员往往为了提高效率以及让代码看起来更加简洁,会使用'包含'函数功能,比如把一系列功能函数都写进function.php中,之后当某个文件需要调用的时候,就直接在文 ...

  9. Linux磁盘修复命令----fsck

    使用fsck命令修复磁盘时 一定要进入单用户模式去修复 语 法fsck.ext4[必要参数][选择参数][设备代号] 功 能fsck.ext4 命令: 针对ext4型文件系统进行检测 参数  -a 非 ...

  10. 【高并发】由InterruptedException异常引发的思考

    写在前面 InterruptedException异常可能没你想的那么简单! 前言 当我们在调用Java对象的wait()方法或者线程的sleep()方法时,需要捕获并处理InterruptedExc ...