POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)

题意:给你m个病毒串,要求长度为n,不包含病毒串的个数。m (0 <= m <= 10), n (1 <= n <=2000000000)


代码:AC自动机本身其实也可以看作是个状态机,每个节点可以向多个方向转移,然后根据状态间能不能转移。对于这题,要求长度为n,所以我们可以构造一个矩阵表示每个点一步能转移到的节点,矩阵的n次幂后,第一行的和就是起点出发走n步的方案数,也就是构成长为n的方案数。

因为不能包含病毒,所以危险结点要去掉,也就是去掉向危险结点的转移。还有比如结点3和4是单词结尾所以危险,

结点2的fail指针指向4,当匹配”AC”时也就匹配了”C”,所以2也是危险的。也就是要把fail指向危险节点的也标记为危

险(对于有疾病的序列,我们可以标记下来最后一个节点,需要注意的是,对于一个节点,如果其fail指针指向的是标记节点,那么这个节点也要被标记,因为fail指向的序列一定是当前序列的后缀。)。

(盗张图,他博客里讲的很好 点击打开链接)


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

/****************************************************/
const int LetterSize = 4;
const int TrieSize = 1000+5;

int tot, root, fail[TrieSize], val[TrieSize], Next[TrieSize][LetterSize];
int newnode(void)
{
    memset(Next[tot], -1, sizeof(Next[tot]));
    val[tot] = 0;
    return tot++;
}

void init(void)
{
    tot = 0;
    root = newnode();
}

int getidx(char x)
{
    if(x == 'A') return 0;
    if(x == 'C') return 1;
    if(x == 'T') return 2;
    if(x == 'G') return 3;
}

void insert(char *ss)
{
    int len = strlen(ss);
    int now = root;
    for(int i = 0; i < len; i++)
    {
        int idx = getidx(ss[i]);
        if(Next[now][idx] == -1)
            Next[now][idx] = newnode();
        now = Next[now][idx];
    }
    val[now] = 1;
}

void build()
{
    queue<int> Q;
    fail[root] = root;
    for(int i = 0; i < LetterSize; 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(val[fail[now]])  //
            val[now] = 1;
        for(int i = 0; i < LetterSize; 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]);
            }
        }
    }
}

int match(char *ss)
{
    int len = strlen(ss), now = root, res = 0;
    for(int i = 0; i < len; i++)
    {
        int idx = getidx(ss[i]);
        int tmp = now = Next[now][idx];
        while(tmp)
        {
            res += val[tmp];
            val[tmp] = 0;
            tmp = fail[tmp];
        }
    }
    return res;
}

/*************************************************/
typedef long long ll;
const int maxn = 1e6+5;
const int mod = 1e5;
char str[maxn];
struct node
{
    ll s[105][105];
}base;

void buildMatrix()
{
    memset(base.s, 0, sizeof(base.s));
    for(int i = 0; i < tot; i++)
        for(int j = 0; j < LetterSize; j++)
            if(!val[i] && !val[Next[i][j]])
                base.s[i][Next[i][j]]++;
}

node mul(node a, node b)
{
    node t;
    memset(t.s, 0, sizeof(t.s));
    for(int i = 0; i < tot; i++)
        for(int j = 0; j < tot; j++)
            for(int k = 0; k < tot; k++)
                t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j])%mod;
    return t;
}

node mt_pow(node p, int q)
{
    node ans;
    memset(ans.s, 0, sizeof(ans.s));
    for(int i = 0; i < tot; i++)
        ans.s[i][i] = 1;
    while(q)
    {
        if(q%2) ans = mul(ans, p);
        p = mul(p, p);
        q /= 2;
    }
    return ans;
}

int main(void)
{
    int m, n;
    while(cin >> m >> n)
    {
        init();
        while(m--)
        {
            scanf(" %s", str);
            insert(str);
        }
        build();
        buildMatrix();
        node ans = mt_pow(base, n);
        ll res = 0;
//        cout << tot << val;
        for(int i = 0; i < tot; i++)
            res = (res+ans.s[0][i])%mod;
        printf("%lld\n", res);
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值