POJ 2778 ac机+矩阵快速幂+重载运算符

http://poj.org/problem?id=2778

说一下具体思路吧,题意好理解,给你几个带病毒的DNA,之后问你长为M的串有几个不带病毒

给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值
这个离散数学中学过,图的矩阵表示,把图变成矩阵,节点间有边则为1,反之为0;
那么,设矩阵M,M^n后,M中M[i][j] 表示从 i–>j 走n步有几种走法;

把给定的图转为邻接矩阵,即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即可。

其次

对于有疾病的序列,我们可以标记下来最后一个节点,需要注意的是,对于一个节点,如果其fail指针指向的是标记节点,那么这个节点也要被标记,因为fail指向的序列一定是当前序列的后缀。标记完后,开始建邻接矩阵,凡是以标记节点作为结尾的边权值都为0(不允许到达)。

最后
对于得到的邻接矩阵跑矩阵快速幂然后统计从根节点到每一个节点的种数之和。

#include <cmath>
#include <queue>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x3f3f3f3f
#define MME(i,j) memset(i,j,sizeof(i))
using namespace std;
const int maxn = 15;
const int mod = 1e5;
int m,k;char str[15];
int limted;
struct matrix{LL a[111][111];}X;
matrix operator * (matrix a,matrix b)
{
    matrix c;
    MME(c.a,0);
    for(int k=0; k<limted; k++)
        for(int i=0; i<limted; i++) //实现矩阵乘法
        {
            for(int j=0; j<limted; j++)
            {
               c.a[k][i]=(c.a[k][i]+a.a[k][j]*b.a[j][i])%mod;
            }
        }

    return c;
}

matrix operator ^ (matrix a,LL b)
{
    matrix c;
    MME(c.a,0);
    for(int i=0;i<limted;i++)
        c.a[i][i]=1;
    while(b)
    {
        if(b&1) c= c * a ;
        b >>= 1;
        a = a * a ;
    }
    return c;
}
int ch(char c)
{
    if(c=='A') return 0;if(c=='C') return 1;
    if(c=='G') return 2;if(c=='T') return 3;
}
struct Trie
{
    int next[111][4],fail[111],end[111];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 4;i++)
            next[L][i] = -1;
        end[L++] = 0;
        return L-1;
    }
    void init(){L = 0;root = newnode();}
    void insert(char buf[])
    {
        int len = strlen(buf);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][ch(buf[i])] == -1)
                next[now][ch(buf[i])] = newnode();
            now = next[now][ch(buf[i])];
        }
        end[now]=1;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 4;i++)
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while( !Q.empty() )
        {
            int now = Q.front();
            Q.pop();
            if(end[fail[now]])
                end[now]=1;
            for(int i = 0;i < 4;i++)
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
}ac;


matrix getmatrix(matrix A)
{
    memset(A.a,0,sizeof(A));
    for(int i=0;i<ac.L;i++)
        for(int j=0;j<4;j++)
            if(!ac.end[ac.next[i][j]])
                A.a[i][ac.next[i][j]]++;
    return A;
}
int main()
{
    int i;
    while(~scanf("%d%d",&m,&k))
    {
        ac.init();
        for(i=1;i<=m;i++)
        {
            scanf("%s",str);
            ac.insert(str);
        }
        ac.build();
        limted=ac.L;
        X = getmatrix(X);
        X=X^k;
        LL ans = 0;
        for(i=0;i<ac.L;i++) ans = (ans + X.a[0][i])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值