poj 2778 _DNA Sequence (AC自动机+矩阵快速幂)

传送门:http://poj.org/problem?id=2778

DNA Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17349 Accepted: 6686

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
题目大意:

给出m个长度在10以内的字符串,这m个串是病毒串,然后问有多少种长度为n的不包含这m个病毒串的串。串只包含4个字符(A,T,C,G)

题解:

这个题涉及的东西还是挺多的,单是那个矩阵的步数那怎么回事就很难理解(一会会提及)。先说说为什么用AC自动机吧?这里主要是借用字典树加上失配指针的应用(这方面的知识这里不详述)。先将给出的m歌串加到字典树上,然后建失配指针。在建失配指针的时候要对每个节点都要保证他有4个儿子节点(A,C,T,G)。如果他没有某个儿子怎么给他附上儿子节点呢?那就让它指向失配指针所指节点的相应的儿子。如果失配指针所指节点的相应的儿子也没有怎么办呢?那就指向根节点。这样做的目的是保证从根节点出发能走到某个字符。至于为什么指向失配指针节点的相应的儿子呢?这就是失配指针的作用了,因为失配节点的后缀是失配指针所指节点的前缀,这样已连接就是一个串的意思了(这里描述的不准确,仅供参考,多了解一下失配指针就能知道什么意思了)。

然后给出一个矩阵大神的结论:

经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值
    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),

实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。

同理,如果要求经过k步的路径数,我们只需要二分求出A^k即可。


这个结论也不知道是怎么证明的了,自己的资历还是达不到。其实在字典树中根节点就代表的是空串,那从空串走一步能

到达每个节点的方案数是多少呢?那就是到A,到C,到T,到C,到G,是四种方案。当然了可能会有不满足的方案数,比如若“C”是病毒

串的话那么从空串到C时不满足要求的,把所有不满足要求的都去掉。还有一种情况就用到是配指针了。例如有病毒串ACG和C,

这样的话第一个串的C的失配指针指向第二个串的C。显然AC是不符合要求的,因为有C。也就是说如果失配指针所指向的节点是病毒

串的话那么该节点也应该被标记为病毒串。

先在就可以开始建矩阵了,从节点i->j走一步的方案数可以求得(符合要求的),那走n步呢?就是那个矩阵相乘了。最后输出

谁的方案数的,应该是从空串(根节点)到其他节点的方案数了。


以上是自己在做这个题的时候所遇到的问题,由于文笔有限所以写的比较感性写,不够条理,还希望读者只提取

有用的信息就可以了。

可以结合这个博客看看(有图就是看起来舒服,也一目了然):http://blog.csdn.net/morgan_xww/article/details/7834801

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#define N 100005
#define MOD 100000
#define LL long long
using namespace std;
struct node{
    int is,id;
    node *fail;
    node *nex[4];
};
node *root,f[N];
int idx ;
struct node* init(){
    node *p;
    p = &f[idx];
    for(int i=0; i<4; i++)
        p->nex[i] = NULL;
    p->is = 0;
    p->id = idx++;
    return p;
};
int ID(char ch){
    if(ch == 'A')return 0;
    else if(ch == 'T')return 1;
    else if(ch == 'C')return 2;
    return 3;
}
char s[20][20];
void build(char *ss){
    int len=strlen(ss);
    node *p = root;
    for(int i= 0 ; i<len; i++){
        int x = ID(ss[i]);
        if(p->nex[x]==NULL)
            p->nex[x] = init();
        p = p->nex[x];
    }
    p->is = 1;
}
void getfail(){
    queue< node* > q;
    q.push(root);
    while(!q.empty()){
        node *p = q.front();
        q.pop();
        for(int i = 0; i<4; i++){
            if(p->nex[i]==NULL){
                if(p==root)
                    p->nex[i] = root;
                else p->nex[i] = p->fail->nex[i];
            }
            else{
                if(p==root)
                    p->nex[i]->fail = root;
                else{
                    p->nex[i]->fail = p->fail->nex[i];
                    if(p->fail->nex[i]->is)
                        p->nex[i]->is = 1;
                }
                q.push(p->nex[i]);
            }
        }
    }
}
struct Matrix{
    LL a[120][120];//矩阵大小根据需求修改
    Matrix(){
        memset(a,0,sizeof(a));
    }
    void init(){
        for(int i=0; i<idx; i++)
            for(int j=0; j<idx; j++)
                a[i][j]=(i==j);
    }
    Matrix operator * (const Matrix &B)const{
        Matrix C;
        for(int i=0; i<idx; i++)
            for(int k=0; k<idx; k++)
                for(int j=0; j<idx; j++){
                    C.a[i][j]=(C.a[i][j]+a[i][k]*B.a[k][j]);
                    if(C.a[i][j]>=MOD)C.a[i][j]%=MOD;
                }
        return C;
    }
    Matrix operator ^ (const LL &t)const{
        Matrix A=(*this),res;
        res.init();  ///矩阵的单位矩阵初始化
        int p=t;
        while(p){
            if(p&1)res=res*A;
            A=A*A;
            p>>=1;
        }
        return res;
    }
};
int main(){
    int m;
    LL n;
    while(~scanf("%d%lld",&m,&n)){
        idx = 0;
        root = init();
        for(int i=1; i<=m; i++){
            scanf("%s",s[i]);
            build(s[i]);
        }
        getfail();
        Matrix ans,re;
        for(int i=0;i<idx;i++){
            for(int j = 0;j<4;j++){
                node *son = f[i].nex[j];
                if(!f[i].is && !son->is){
                    ans.a[i][son->id]++;
                }
            }
        }
        re = ans^n;
        LL sum = 0;
        for(int i=0;i<idx;i++){
            sum+=re.a[0][i];
            if(sum>=MOD)sum%=MOD;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值