DNA Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15253 Accepted: 5890
Description
It’s well known that DNA Sequence is a sequence only contains A, C, T and G, and it’s very useful to analyze a segment of DNA Sequence,For example, if a animal’s DNA sequence contains segment ATC then it may mean that the animal may have a genetic disease. Until now scientists have found several those segments, the problem is how many kinds of DNA sequences of a species don’t contain those segments.
Suppose that DNA sequences of a species is a sequence that consist of A, C, T and G,and the length of sequences is a given integer n.
Input
First line contains two integer m (0 <= m <= 10), n (1 <= n <=2000000000). Here, m is the number of genetic disease segment, and n is the length of sequences.
Next m lines each line contain a DNA genetic disease segment, and length of these segments is not larger than 10.
Output
An integer, the number of DNA sequences, mod 100000.
Sample Input
4 3
AT
AC
AG
AA
Sample Output
36
建立trie图的邻接矩阵M:
2 1 0 0 1
2 1 1 0 0
1 1 0 1 1
2 1 0 0 1
2 1 0 0 1
M[i,j]表示从结点i到j只走一步有几种走法。
那么M的n次幂就表示从结点i到j走n步有几种走法
博主语言表达能力弱,好文地址
code:
#include <cstdio>
#include <queue>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int MAX_N = 10 * 10 + 5;
const int CLD_NUM = 4;
typedef long long MATRIX[MAX_N][MAX_N];
MATRIX mat, mat1, mat2;
long long (*m1)[MAX_N], (*m2)[MAX_N];
class acAutomaton
{
public:
int n;
int id['Z'+1];
int fail[MAX_N];
bool tag[MAX_N];
int trie[MAX_N][CLD_NUM];
void init()
{
id['A'] = 0;
id['T'] = 1;
id['C'] = 2;
id['G'] = 3;
}
void reset()
{
memset(trie[0], -1, sizeof(trie[0]));
tag[0] = false;
n = 1;
}
void add(char *s)
{
int p = 0;
while (*s)
{
int i = id[*s];
if ( -1 == trie[p][i] )
{
memset(trie[n], -1, sizeof(trie[n]));
tag[n] = false;
trie[p][i] = n++;
}
p = trie[p][i];
s++;
}
tag[p] = true;
}
void construct()
{
queue<int> Q;
fail[0] = 0;
for (int i = 0; i < CLD_NUM; i++)
{
if (-1 != trie[0][i])
{
fail[trie[0][i]] = 0;
Q.push(trie[0][i]);
}
else
{
trie[0][i] = 0;
}
}
while ( !Q.empty() )
{
int u = Q.front();
Q.pop();
if (tag[fail[u]])
tag[u] = true;
for (int i = 0; i < CLD_NUM; i++)
{
int &v = trie[u][i];
if ( -1 != v )
{
Q.push(v);
fail[v] = trie[fail[u]][i];
}
else
{
v = trie[fail[u]][i];
}
}
}
}
void buildMatrix()
{
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n; i++)
for (int j = 0; j < CLD_NUM; j++)
if ( !tag[i] && !tag[trie[i][j]] )
mat[i][trie[i][j]]++;
}
} ac;
void matrixMult(MATRIX t1, MATRIX t2, MATRIX res)
{
for (int i = 0; i < ac.n; i++)
for (int j = 0; j < ac.n; j++)
{
res[i][j] = 0;
for (int k = 0; k < ac.n; k++)
{
res[i][j] += t1[i][k] * t2[k][j];
}
res[i][j] %= 100000;
}
}
void matrixPower(int p)
{
if (p == 1)
{
for (int i = 0; i < ac.n; i++)
for (int j = 0; j < ac.n; j++)
m2[i][j] = mat[i][j];
return;
}
matrixPower(p/2);
matrixMult(m2, m2, m1);
if (p % 2)
matrixMult(m1, mat, m2);
else
swap(m1, m2);
/*for (int i = 0; i < ac.n; i++)
for (int j = 0; j < ac.n; j++)
m1[i][j] = mat[i][j];
for(int k=0;k<p-1;k++)
{
for (int i = 0; i < ac.n; i++)
for (int j = 0; j < ac.n; j++)
m2[i][j] = mat[i][j];
matrixMult(m2, m1, mat);
}*/
}
int main()
{
int n, m;
char s[12];
ac.init();
cin >> m >> n;
ac.reset();
while ( m-- )
{
scanf("%s", s);
ac.add(s);
}
ac.construct();
ac.buildMatrix();
m1 = mat1;
m2 = mat2;
matrixPower(n);
int ans = 0;
for (int i = 0; i < ac.n; i++)
ans += m2[0][i];
printf("%d\n",ans % 100000);
return 0;
}