【第22期】观点:IT 行业加班,到底有没有价值?

poj 2778 ac自动机+矩阵乘法

原创 2016年08月29日 15:33:05

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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

题意:给出n个字串表示“缺陷基因”,然后让求长度为m的基因(4^m个)中有多少个不带病。 题解:首先建立AC自动机,然后从每个节点开始选“ATGC”有四种往外转移的途径。 如:ACG,C这...

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

用AC自动机构造不含模式串的模板题,与其说是用AC自动机不如说就是用Trie 首先我们把所有的模式串(给定的DNA序列)建Trie,假定我们有一个匹配串,并且在匹配过程到S[i]这个字符时匹配到了T...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

poj2778 ac自动机+矩阵

此题ac的我莫名其妙的的。。。 http://blog.csdn.net/kk303/article/details/6936046此blog说了比较要注意的地方 由于我的模版是数组模拟链表的。所...

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

DNA SequenceTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5080 Accepted: 1766Description...

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...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)