poj 2778 DNA Sequence 【AC自动机 + 矩阵加速】

DNA Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13502 Accepted: 5143

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


题意:给你n个DNA模式串,若DNA序列中含有模式串,则说明患病。现在问你m长度的DNA序列中有多少种不患病的。


建议看下这道题——离散数学的应用:点我


注意:要理解AC自动机里面状态转移的前提。若fail[now]指针没有指向root,说明fail[now]指向节点序列的前缀是当前节点序列的某个后缀。


思路:构建出AC自动机的trie,标记所有模式串的序列结点。在BFS建状态图时,若trie上某个节点u通过失配指针可以到达某个模式串,则说明此处不通,标记。 最后构建矩阵,行列均为trie节点数,求出矩阵的m次幂,累加0节点即根到trie上所有节点的矩阵值就是答案。


因为0节点是trie的根,在矩阵所表示的图中是不存在的,所以求出矩阵的m次幂才是m长度串的所有数目,而不是m-1次幂。

测试数据没过,原来这里求的是矩阵的m-1次幂 o(╯□╰)o



AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 120
#define LL long long
#define MOD 100000
using namespace std;
struct Matrix{
    LL a[MAXN][MAXN];
    int N;
};
Matrix ori, res;
void init_a(int NN)
{
    memset(ori.a, 0, sizeof(ori.a));
    memset(res.a, 0, sizeof(res.a));
    ori.N = res.N = NN;
    for(int i = 0; i < NN; i++)
        res.a[i][i] = 1;
}
struct Trie
{
    int next[MAXN][4], fail[MAXN], End[MAXN];
    int L, root;
    int newnode()
    {
        for(int i = 0; i < 4; i++)
            next[L][i] = -1;
        End[L++] = 0;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    int getval(char op)
    {
        if(op == 'A') return 0;
        if(op == 'C') return 1;
        if(op == 'G') return 2;
        if(op == 'T') return 3;
    }
    void Insert(char *s)
    {
        int len = strlen(s);
        int now = root;
        for(int i = 0; i < len; i++)
        {
            int v = getval(s[i]);
            if(next[now][v] == -1)
                next[now][v] = newnode();
            now = next[now][v];
        }
        End[now] = 1;
    }
    void Build()
    {
        queue<int> Q;
        fail[root] = root;
        for(int i = 0; i < 4; i++)
        {
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            if(End[fail[now]] == 1)//失配边所指 为模式串
                End[now] = 1;//此处不通
            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];
                    Q.push(next[now][i]);
                }
            }
        }
    }
    void getMatrix()
    {
        for(int i = 0; i < L; i++)
            if(End[i] == 0)
                for(int j = 0; j < 4; j++)
                    if(End[next[i][j]] == 0)//所有通路
                        ori.a[i][next[i][j]]++;
    }
};
Trie ac;
Matrix multi(Matrix x, Matrix y)
{
    Matrix z;
    memset(z.a, 0, sizeof(z.a));
    z.N = x.N;
    for(int i = 0; i < x.N; i++)
    {
        for(int k = 0; k < y.N; k++)
        {
            if(x.a[i][k] == 0) continue;
            for(int j = 0; j < x.N; j++)
                z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % MOD) % MOD;
        }
    }
    return z;
}
void solve(int n)
{
    while(n)
    {
        if(n & 1)
            res = multi(ori, res);
        ori = multi(ori, ori);
        n >>= 1;
    }
    LL ans = 0;
    for(int i = 0; i < res.N; i++)
        ans = (ans + res.a[0][i]) % MOD;
    printf("%lld\n", ans);
}
char str[120];
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        ac.init();
        for(int i = 0; i < n; i++)
            scanf("%s", str), ac.Insert(str);
        ac.Build();
        init_a(ac.L);
        ac.getMatrix();
        solve(m);
    }
    return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值