poj 2778 ac自动机+矩阵乘法

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

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只走一步有几种走法。

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;
}
{
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.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;
}

• 本文已收录于以下专栏：

Poj 2778 [AC自动机，矩阵乘法]

-----我的AC自动机----- •题意：有m种DNA序列是有疾病的，问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列。（仅含A,T,C,G四个字符） •样例m=4,n=3...

poj2778 DNA Sequence AC自动机 dp 矩阵乘法

poj2778 DNA Sequence 题意:求仅有A G C T不含有k(k
• yxr0105
• 2016年02月03日 01:02
• 185

poj2778 DNA Sequence AC自动机+矩阵乘法

DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15900 Accepted...

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...

poj 2778 DNA Sequence //AC自动机+矩阵乘法

DNA SequenceTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5080 Accepted: 1766Description...
• hqd_acm
• 2011年04月22日 21:59
• 1227

【POJ2778】AC自动机，DP，矩阵乘法

• Vmurder
• 2014年09月17日 08:13
• 1082

POJ 2778 DNA Sequence （AC自动机+矩阵加速，4级）

E - DNA Sequence Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536...

举报原因： 您举报文章：poj 2778 ac自动机+矩阵乘法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)