POJ 2778(AC自動機+矩陣快速冪)

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個病毒串,問長度爲m的DNA序列中有多少種是不包含病毒串的。

解題思路:我們將病毒串加入AC自動機中,然後我們先創建一個矩陣表示從i點走到j點的長度爲1的字符串中不包含病毒串的種類數。我們知道,如果從i點到j點的長度爲2的字符串可以由從i點到k點長度爲1的字符串和從k點到j點長度爲1的字符串拼接得到,所以其實我們列出來所有的k就可以看出這是一個矩陣乘法公式。所以我們將這個矩陣乘以m次就可以啦。然後計算從0點到其他點的種類數。

代碼:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <queue>
//#include <random>
#include <time.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ls root<<1
#define rs root<<1|1
const int inf=1ll*1<<60;
const int maxn=1e4+10;
const int maxm=1e6+10;
const int mod=100000;
char arr[110];
int trie[110][4],fail[110],tot,val[110],idx[128];
void add(char *s)
{
    int now=0;
    for(int i=0;s[i]!='\0';i++){
        int x=idx[s[i]];
        if(!trie[now][x]) trie[now][x]=++tot;
        now=trie[now][x];
    }
    val[now]=1;
}
void getfail()
{
    queue<int> q;
    for(int i=0;i<4;i++)
        if(trie[0][i])q.push(trie[0][i]);
    while(!q.empty()){
        int now=q.front();q.pop();
        for(int i=0;i<4;i++){
            if(trie[now][i]){
                fail[trie[now][i]]=trie[fail[now]][i];
                q.push(trie[now][i]);
            }
            else trie[now][i]=trie[fail[now]][i];
            val[trie[now][i]]|=val[trie[fail[now]][i]];
        }
    }
}
struct martix
{
    int mat[110][110];
    martix(){
        memset(mat,0,sizeof mat);
    }
};
martix operator *(const martix a,const martix b)
{
    martix z;
    for(int i=0;i<=tot;i++){
        for(int j=0;j<=tot;j++){
            for(int k=0;k<=tot;k++){
                z.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                z.mat[i][j]%=mod;
            }
        }
    }
    return z;
}
signed main()
{
    int n,m;
    scanf("%lld%lld",&n,&m);
    idx['A']=0,idx['C']=1,idx['G']=2,idx['T']=3;
    for(int i=0;i<n;i++){
        scanf("%s",arr);
        add(arr);
    }
    getfail();
    martix e,x;
    for(int i=0;i<=tot;i++){
        e.mat[i][i]=1;
    }
    for(int i=0;i<=tot;i++){
        if(val[i])continue;
        for(int j=0;j<4;j++){
            if(val[trie[i][j]])continue;
            x.mat[i][trie[i][j]]++;
        }
    }
    while(m){
        if(m&1)e=e*x;
        x=x*x;
        m>>=1;
    }
    int ans=0;
    for(int i=0;i<=tot;i++){
        ans+=e.mat[0][i];
        ans%=mod;
    }
    printf("%lld\n",ans);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章