0%

POJ 2778: DNA Sequence

题意

求构造不含m(0 <= m <= 10)个模式串的长度为n(1 <= n <= 2000000000)的字符串的方案数。

分析

首先用m个模式串构造AC自动机,这里需要注意的是,不仅要把字符串最后一个字符所在的那一点标记为不合法状态,还需要把其他能转移到该点的节点都标记为不合法状态。然后,问题就转化为了从root出发,走了n步,这n步都避开不合法状态的方案数。设AC自动机的节点个数为L,我们可以构造一个L*L的邻接矩阵,mat[i][j]代表i到j的合法方案数。则mat矩阵的n次幂就表示走了n次的方案数。由于n很大,所以要用矩阵快速幂优化。这里要注意的是,每次循环都要将不合法的项置0,否则,有可能会多算。

代码

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

#pragma comment(linker, "/STACK:102400000,102400000") //手动扩栈

#include<set>
#include<map>
#include<ctime> //CLOCKS_PER_SEC;clock_t t=clock();
#include<cmath>
#include<queue>
#include<bitset>
#include<cctype>
#include<cstdio>
#include<vector>
#include<string> //getline(cin, line);
#include<sstream> //stringstream ss(line);(ss is a stream like cin).
#include<cstdlib>
#include<cstring>
#include<cfloat> //X=FLT,DBL,LDBL;X_MANT_DIG,X_DIG,X_MIN_10_EXP,X_MIN_10_EXP,X_MIN,X_MAX,X_EPSILON
#include<climits> //INT_MAX,LLONG_MAX
#include<iostream> //ios_base::sync_with_stdio(false);
#include<algorithm>
#define x first
#define y second
#define ok cout << "ok" << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double PI = acos(-1.0);
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int N=1e5+9;
const int shift=1e3+9;
const double Eps=1e-7;

const int mod = 100000;
map<char, int> m;
int n, mm;
char s[19];

struct acAuto {
int next[109][4], fail[109], L, root;
ll mat[109][109];
bool end[109];
void init() {
L = 0;
m['A'] = 0;
m['T'] = 1;
m['C'] = 2;
m['G'] = 3;
root = newNode();
}
int newNode() {
for(int i = 0; i < 4; i++)
next[L][i] = -1;
end[L] = false;
return L++;
}
void insert(char s[]) {
int len = strlen(s), now = root;
for(int i = 0; i < len; i++) {
int j = m[s[i]];
if(next[now][j] == -1)
next[now][j] = newNode();
now = next[now][j];
}
end[now] = true;
}
void build() {
fail[root] = root;
queue<int> que;
for(int i = 0; i < 4; i++) {
if(next[root][i] == -1)
next[root][i] = root;
else {
fail[next[root][i]] = root;
que.push(next[root][i]);
}
}
while(que.size()) {
int now = que.front();
que.pop();
for(int i = 0; i < 4; i++) {
if(next[now][i] == -1) {
next[now][i] = next[fail[now]][i];
}
else {
fail[next[now][i]] = next[fail[now]][i];
que.push(next[now][i]);
}
}
}
}
void buildMatrix() {
memset(mat, 0, sizeof mat);
for(int i = 0; i < L; i++) {
int now = i, flag = false;
while(now != root) {
if(end[now]) flag = true;
now = fail[now];
}
if(flag)
end[i] = true;
}
for(int i = 0; i < L; i++)
for(int j = 0; j < 4; j++)
mat[i][next[i][j]]++;
}
void ksm(int n) {
ll t[109][109];
ll t1[109][109];
ll t2[109][109];
memset(t, 0, sizeof t);
for(int i = 0; i < L; i++)
t[i][i] = 1;
while(n) {
for(int j = 0; j < L; j++)
if(end[j] == true)
for(int i = 0; i < L; i++)
mat[i][j] = t[i][j] = 0;
if(n & 1) {
memcpy(t1, t, sizeof t);
memset(t, 0, sizeof t);
for(int i = 0; i < L; i++)
for(int j = 0; j < L; j++)
for(int k = 0; k < L; k++)
(t[i][j] += t1[i][k] * mat[k][j]) %= mod;
}
n >>= 1;
memcpy(t2, mat, sizeof mat);
memset(mat, 0, sizeof mat);
for(int i = 0; i < L; i++)
for(int j = 0; j < L; j++)
for(int k = 0; k < L; k++)
(mat[i][j] += t2[i][k] * t2[k][j]) %= mod;
}
for(int i = 0; i < L; i++)
for(int j = 0; j < L; j++)
mat[i][j] = t[i][j];
}
void solve() {
buildMatrix();
ksm(n);
ll ans = 0;
for(int j = 0; j < L; j++)
(ans += mat[0][j]) %= mod;
printf("%lld\n", ans);
}
}ac;

int main(void) {
if(fopen("in", "r")!=NULL) {freopen("in", "r", stdin); freopen("out", "w", stdout);}
while(~scanf("%d%d", &mm, &n)) {
ac.init();
for(int i = 0; i < mm; i++) {
scanf("%s", s);
ac.insert(s);
}
ac.build();
ac.solve();
};
return 0;
}