【POJ 2778 DNA Sequence】 AC自动机+DP+矩阵快速幂

POJ2778
本题题意是给你一个字符集和一个长度m,还有一些敏感串,求出字符集构造出的长度为m的字符串中不包含敏感串的串的个数。
我们用到AC自动机的性质,想象一下如果从len=k向len=k+1转移,AC自动机上每个状态之间有多少种转移方法,就可以构造出对应的转移矩阵,再利用矩阵快速幂就可以求解。在构造转移矩阵的时候,要注意如果某串的后缀是敏感串,也是不可以转移的。
转移矩阵 T n T [ i ] [ j ] 即表示i状态转移n次到达j状态的方案数,所以最后统计第一行的和,也就是所有初始状态转移n次得到的状态之和了。
你可能会想到一些原本不在AC自动机上面的状态是不是没有被算,其实所有不在AC自动机上的状态都是不被约束的状态,他们是和从root开始的状态相同的,我们在构建fail的时候就把这些串下标都存为root,所以直接算转移矩阵的时候直接转移就好了。(慢慢理解,慢慢理解
POJ2778代码

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define  dbg(x) cout<<#x<<" "<<x<<endl
typedef unsigned long long ll;
const int maxn = 2e6+5;
const int Mod=100000;
map<char,int> mp;
struct ACTrie
{
    int tree[maxn][4],fail[maxn];
    int end_[maxn];
    int root,num,cnt;
    int newnode()
    {
        for(int i=0;i<4;i++)
            tree[cnt][i]=-1;
        end_[cnt]=0;
        return cnt++;
    }
    void init()
    {
        cnt=0;
        num=0;
        root=newnode();
    }
    void insert_(char str[])
    {
        int pos=root;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            int id=mp[str[i]];
            if(tree[pos][id]==-1) tree[pos][id]=newnode();
            pos=tree[pos][id];
        }
        end_[pos]=1;
    }
    void build()
    {
        queue<int> que;
        fail[root]=root;
        for(int i=0;i<4;i++)
        {
            if(tree[root][i]==-1) tree[root][i]=root;
            else
            {
                fail[tree[root][i]]=root;
                que.push(tree[root][i]);
            }
        }
        while(!que.empty())
        {
            int now=que.front();
            que.pop();
            for(int i=0;i<4;i++)
            {
                if(tree[now][i]==-1) tree[now][i]=tree[fail[now]][i];
                else
                {
                    fail[tree[now][i]]=tree[fail[now]][i];
                    que.push(tree[now][i]);
                }
                end_[tree[now][i]]|=end_[tree[fail[now]][i]];//注意这个过程,若该字符串的后缀为病毒串,则该字符串也是病毒串。
            }
        }
    }
};
ACTrie ac;
struct mat
{
    ll jz[110][110];
};
mat make_mat()
{
    mat res;
    memset(res.jz,0,sizeof(res.jz));
    for(int i=0;i<ac.cnt;i++)
    {
        if(ac.end_[i]) continue;//转移之前为病毒串不统计
        for(int j=0;j<4;j++)
        {
            if(ac.end_[ac.tree[i][j]]) continue;//转移之后为病毒串不统计
            ++res.jz[i][ac.tree[i][j]];
        }
    }
    return res;
}
mat mat_mul(mat x,mat y)
{
    mat res;
    memset(res.jz,0,sizeof(res.jz));
    for(int i=0;i<ac.cnt;i++)
        for(int j=0;j<ac.cnt;j++)
            for(int k=0;k<ac.cnt;k++)
                res.jz[i][j]=(res.jz[i][j]+x.jz[i][k]*y.jz[k][j]+Mod)%Mod;
        return res;
}
ll power_mod (ll b)//.res是系数矩阵,ans是变换矩阵左->ans,右->res.
{
    mat ans,res;
    res=make_mat();
    memset(ans.jz,0,sizeof(ans.jz));
    for(int i=0;i<ac.cnt;i++)
        ans.jz[i][i]=1;
    while(b>0)
    {
        if(b&1)  ans=mat_mul(res,ans);//所以应该系数矩阵在前ans,res);
        b=b>>1;
        res=mat_mul(res,res);
    }
    ll tmp=0;
    for(int i=0;i<ac.cnt;i++)
        tmp=(tmp+ans.jz[0][i]%Mod)%Mod;
    return tmp;//返回指定位置元素
}
char str[11];
int main()
{
    mp['A']=0;
    mp['C']=1;
    mp['T']=2;
    mp['G']=3;
    int n;
    ll m;
    while(scanf("%d%I64d",&n,&m)!=EOF)
    {
        ac.init();
        while(n--)
        {
            scanf("%s",str);
            ac.insert_(str);
        }
        ac.build();
        printf("%I64d\n",power_mod(m));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38891827/article/details/80684127