现在的位置: 首页 > 综合 > 正文

poj2778 AC自动机+矩阵快速幂

2018年02月23日 ⁄ 综合 ⁄ 共 1793字 ⁄ 字号 评论关闭

推荐博客:这里

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const long long M=100000;
struct matrix
{
    long long a[111][111];
};
int ch[111][4],val[51111];
int f[51111];
int sz,n,m;
char str[22];
map<char,int>p;
void init()
{
    sz=0;
    memset(ch[0],0,sizeof(ch[0]));
}
void insert(char *a,int vv)
{
    int u=0,l=strlen(a);
    for(int i=0; i<l; i++)
    {
        int c=p[a[i]];
        if(!ch[u][c])
        {
            ch[u][c]=++sz;
            memset(ch[sz],0,sizeof(ch[sz]));
            val[sz]=0;
        }
        u=ch[u][c];
    }
    val[u]=vv;
}
void getfail()
{
    queue<int>q;
    f[0]=0;
    for(int i=0; i<4; i++)
    {
        int u=ch[0][i];
        if(u)
        {
            f[u]=0;
            q.push(u);
        }
    }
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int u=ch[r][i];
            if(!u)continue;
            q.push(u);
            int v=f[r];
            while(v&&!ch[v][i])v=f[v];
            f[u]=ch[v][i];
            if(val[f[u]])val[u]=val[f[u]];
        }
    }
}
matrix buildmatrix()
{
    matrix mat;
    memset(mat.a,0,sizeof(mat.a));
    queue<int>q;
    q.push(0);
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int u=ch[r][i];
            if(u)
            {
                if(!val[u])
                {
                    mat.a[r][u]++;
                    q.push(u);
                }
            }
            else
            {
                int v=f[r];
                while(v&&!ch[v][i])v=f[v];
                int vv=ch[v][i];
                if(!val[vv])mat.a[r][vv]++;
            }
        }
    }
    return mat;
}
matrix mul(matrix x,matrix y)
{
    matrix c;
    for(int i=0;i<=sz;i++)
    {
        for(int j=0;j<=sz;j++)c.a[i][j]=0;
    }
    for(int i=0;i<=sz;i++)
    {
        for(int j=0;j<=sz;j++)
        {
            for(int k=0;k<=sz;k++)
            {
                c.a[i][j]+=x.a[i][k]*y.a[k][j];
                c.a[i][j]%=M;
            }
        }
    }
    return c;
}
matrix mi(matrix b,int x)
{
    matrix p,q;
    p=b;
    memset(q.a,0,sizeof(q.a));
    for(int i=0;i<=sz;i++)q.a[i][i]=1;
    while(x!=1)
    {
        if(x%2)
        {
            x--;
            q=mul(p,q);
        }
        else
        {
            x>>=1;
            p=mul(p,p);
        }
    }
    p=mul(p,q);
    return p;
}
int main()
{
    p['A']=0;
    p['C']=1;
    p['G']=2;
    p['T']=3;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        init();
        for(int i=0; i<m; i++)
        {
            scanf("%s",str);
            insert(str,1);
        }
        getfail();
        //cout<<1<<endl;
        matrix mat=buildmatrix();
        /*for(int i=0;i<=sz;i++)
        {
            for(int j=0;j<=sz;j++)printf("%d ",mat.a[i][j]);
            printf("\n");
        }*/
        mat=mi(mat,n);
        long long ans=0;
        for(int i=0;i<=sz;i++)
        {
            ans+=mat.a[0][i];
            ans%=M;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.