AC自动机构造字符串(好)poj2778

本文介绍了一种使用AC自动机结合矩阵快速幂的方法来解决特定DNA序列计数问题。该方法首先通过AC自动机构建模式匹配树,然后利用矩阵快速幂进行高效的指数级计算,从而得出不包含特定子序列的DNA序列数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Language:
DNA Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11630 Accepted: 4439

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的,并且不包含给出的任意一个字符串的串有多少个

思路:首先建立ac自动机,然后构造转移矩阵,下面的博客讲得很清楚,加一点自己的理解在代码里。

http://hi.baidu.com/ccsu_010/item/7847a3c17f6fe2bc0d0a7b89

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=15;
const int maxm=15*10;
const int SIGMA_SIZE=4;
const int MOD=100000;
char word[20];
int n,m,sz;
int cnt[110][110];

struct Matrix
{
    int mat[110][110];
    Matrix(){memset(mat,0,sizeof(mat));}
    Matrix operator*(Matrix a)
    {
        Matrix res;
        for(int k=0;k<sz;k++)
        for(int i=0;i<sz;i++)
        {
            if(mat[i][k]==0)continue;
            for(int j=0;j<sz;j++)
                if(a.mat[k][j])
                    res.mat[i][j]=(res.mat[i][j]+(LL)mat[i][k]*a.mat[k][j]%MOD)%MOD;
        }

        return res;
    }
};
struct AC
{
    int ch[maxm][4],val[maxm];
    int fail[maxm],last[maxm];

    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;val[0]=0;}
    int idx(char x)
    {
        if(x=='A')return 0;
        else if(x=='C')return 1;
        else if(x=='T')return 2;
        return 3;
    }
    void insert(char *s)
    {
        int u=0;
        int n=strlen(s);
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=1;
    }
    void getfail()
    {
        queue<int> q;
        int u=0;
        fail[0]=0;
        for(int i=0;i<SIGMA_SIZE;i++)
        {
            u=ch[0][i];
            if(u){fail[u]=last[u]=0;q.push(u);}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            if(val[fail[r]])val[r]=1;//失配指针相连的,若果fail[u]是单词节点,那么相应的当前节点也是单词节点,因为fail[u]是这个的前缀
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                u=ch[r][c];
                if(!u){ch[r][c]=ch[fail[r]][c];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][c])v=fail[v];
                fail[u]=ch[v][c];
                last[u]=val[fail[u]]?fail[u]:last[fail[u]];
            }
        }
    }
    Matrix getMatrix()//根据是否是单词节点构造矩阵,进行转移
    {
        Matrix res;
        for(int i=0;i<sz;i++)
            for(int j=0;j<4;j++)
                if(!val[ch[i][j]])
                    res.mat[i][ch[i][j]]++;
        return res;
    }

}ac;
Matrix pow_mul(Matrix A,int x)
{
    Matrix res;
    for(int i=0;i<=sz;i++)res.mat[i][i]=1;
    while(x)
    {
        if(x&1)res=res*A;
        A=A*A;
        x>>=1;
    }
    return res;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ac.clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",word);
            ac.insert(word);
        }
        ac.getfail();
        Matrix A=ac.getMatrix();
        A=pow_mul(A,m);
        int ans=0;
        for(int i=0;i<sz;i++)
            ans=(ans+A.mat[0][i])%MOD;
        printf("%d\n",ans);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值