[POJ 1625] Censored! (AC自动机+DP+高精度)

链接

POJ 1625


题意

给出P个模式串,问长度为M且不含有P中任何一个为子串的字符串有多少种。
给出了大小为N的一个字符集,属于ASCII但不一定为英文字母。
最终答案不进行取模,所以可能非常大。


题解

给出模式串找不含其中任何一个的串的种类数,这类的题目大多可以用trie图+矩阵快速幂解决。但是这道题目的答案不取模,显然要用高精度,如果用在矩阵快速幂中套用高精度。。。感觉即使不MLT也TLT了,所以这里要使用dp。
从dp的角度看,这道题目的转移很简单,dp[i][j]代表长度为i,位于自动机j节点满足题意的种类数,显然根据trie图很好转移。

感觉trick在于,char类型变量事实上是带符号的类型,所以直接作为数组下标会越界,加上一个基值就可以了。
大整数类感觉mod不要用10了,用10000比较好,节省空间,提高速度。


代码
#include <cstdio>
#include <fstream>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef unsigned char uchar;
#define MAX_L (111)
#define degree (55)
int tsf[1<<9], cnt;
int trieG[MAX_L][MAX_L];
struct Aho
{
    int next[MAX_L][degree], nd[MAX_L], fail[MAX_L];
    int root, L;
    int newnode()
    {
        for(int i = 0; i < degree; i++)
            next[L][i] = -1;
        nd[L] = 0;
        return L++;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char s[])
    {
        //printf("insert\n");
        int now = root;
        for(int i = 0, key, sz = strlen(s); i < sz; i++)
        {
            key = tsf[256 + s[i]];
            if(next[now][key] == -1)
                next[now][key] = newnode();
            now = next[now][key];
        }
        nd[now] = 1;
    }
    void build()
    {
        //printf("build\n");
        queue<int> que;
        fail[root] = root;
        for(int i = 0; i < degree; i++)
        if(next[root][i] == -1)
            next[root][i] = root;
        else
        {
            fail[next[root][i]] = root;
            que.push(next[root][i]);
        }
        while(!que.empty())
        {
            //printf("while\n");
            int now = que.front();
            que.pop();
            if(nd[fail[now]])
                nd[now] = 1;
            for(int i = 0; i < degree; i++)
            if(next[now][i] == -1)
                next[now][i] = next[fail[now]][i];
            else
            {
                fail[next[now][i]] = next[fail[now]][i];
                que.push(next[now][i]);
            }
        }
        //printf("build end\n");
    }
    void gettrieG(int n)
    {
        //printf("getG\n");
        memset(trieG, 0, sizeof(trieG));
        for(int i = 0; i < L; i++)
        for(int j = 0; j < n; j++)
        if(!nd[next[i][j]]) trieG[i][next[i][j]]++;
    }
} actree;
struct bign
{
    #define MAX_B (100)
    #define MOD (10000)
    int a[MAX_B], n;
    bign() { a[0] = 0, n = 1; }
    bign(int num)
    {
        n = 0;
        do {
            a[n++] = num % MOD;
            num /= MOD;
        } while(num);
    }
    bign& operator= (int num)
    { return *this = bign(num); }
    bign operator+ (const bign& b) const
    {
        bign c = bign();
        int cn = max(n, b.n), d = 0;
        for(int i = 0, x, y; i < cn; i++)
        {
            x = (n > i) ? a[i] : 0;
            y = (b.n > i) ? b.a[i] : 0;
            c.a[i] = (x + y + d) % MOD;
            d = (x + y + d) / MOD;
        }
        if(d) c.a[cn++] = d;
        c.n = cn;
        return c;
    }
    bign& operator+= (const bign& b)
    {
        *this = *this + b;
        return *this;
    }
    bign operator* (const bign& b) const
    {
        bign c = bign();
        int cn = n + b.n, d = 0;
        for(int i = 0; i <= cn; i++)
            c.a[i] = 0;
        for(int i = 0; i < n; i++)
        for(int j = 0; j < b.n; j++)
        {
            c.a[i + j] += a[i] * b.a[j];
            c.a[i + j + 1] += c.a[i + j] / MOD;
            c.a[i + j] %= MOD;
        }
        while(cn > 0 && !c.a[cn-1]) cn--;
        if(!cn) cn++;
        c.n = cn;
        return c;
    }
    friend ostream& operator<< (ostream& _cout, const bign& num)
    {
        printf("%d", num.a[num.n - 1]);
        for(int i = num.n - 2; i >= 0; i--)
            printf("%04d", num.a[i]);
        return _cout;
    }
};
#define MAX_M (55)
bign dp[MAX_M][MAX_L];
int main()
{
    int N, M, P;
    char table[100];
    while(cin >> N >> M >> P)
    {
        scanf("%s", table);
        for(int i = 0; i < N; i++)
            tsf[256 + table[i]] = i;
        actree.init();
        for(int i = 0; i < P; i++)
        {
            scanf("%s", table);
            actree.insert(table);
        }
        actree.build();
        actree.gettrieG(N);

        for(int i = 0; i <= M; i++)
        for(int j = 0; j < actree.L; j++)
        {
            dp[i][j] = bign();
        }

        dp[0][0] = 1;
        for(int i = 0; i < M; i++)
        for(int j = 0; j < actree.L; j++)
        for(int k = 0; k < actree.L; k++) {
            dp[i + 1][k] += dp[i][j] * bign(trieG[j][k]);
        }
        bign ans = bign();
        for(int i = 0; i < actree.L; i++)
            ans += dp[M][i];
        cout << ans << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值