POJ 1625 Censored! (AC自动机+DP+大数加法)

题目大意

  • 给出包含N个字符的字典,组成长度为M的句子,求无禁止单词的句子有多少个。禁止单词有P个,单词长度不超过10,所以字符的ASCII码均大于32(1 <= N <= 50, 1 <= M <= 50, 0 <= P <= 10)。

分析

  • 状态dp[i][j]表示第i步时,到达状态j的字符串数,途中不能经过危险结点
  • 则状态转移方程为

    dp[i][j] = sum(dp[i-1][k]),其中j是k的后继状态
    
  • 很明显,所以结果加起来超过long long。所以需要用到大数的加法

  • 构造AC自动机时,失败结点为危险的结点也要变成危险结点。
  • 输入字符的ASCII码可能超过128,所以需要用的映射,不然RE

代码

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>

using namespace std;
const int maxn = 110;
const int N = 51;
const int sigma_size = 50;
const int M = 100;

struct AC {
    int ch[maxn][sigma_size];
    int val[maxn];
    int sz;
    char s[N];
    int idx(char c) {
        int L = 0 , R = strlen(s) - 1;
        while(L <= R)
        {
            int M = (L + R) / 2;
            if(c == s[M]) return M;
            if(c > s[M]) L = M + 1;
            else R = M - 1;
        }
        return L;
    }
    void init() {
        sz=1; val[0]=0;
        cin >> s;
        int len = strlen(s);
        sort(s , s + len);
        memset(ch[0],0,sizeof(ch[0]));
    }
    void insert(string const &s , int v)
    {
        int cur = 0 , len = s.length();
        for(int i = 0; i < len; i++)
        {
            int u = idx(s[i]);
            if(!ch[cur][u]) {
                memset(ch[sz] , 0 , sizeof(ch[sz]));
                val[sz] = 0;
                ch[cur][u] = sz++;
            }
            cur = ch[cur][u];
        }
        val[cur] = v;
    }
    int f[maxn];
    void getfail()
    {
        queue<int> Q; f[0] = 0;
        for(int i = 0; i < sigma_size; i++) {
            int v = ch[0][i];
            if(v) {f[v] = 0; Q.push(v);}
        }
        while(!Q.empty())
        {
            int cur = Q.front(); Q.pop();
            for(int i = 0; i < sigma_size; i++){
                int u = ch[cur][i];
                if(!u) {ch[cur][i] = ch[f[cur]][i]; continue;}
                Q.push(u);
                int j = f[cur];
                while(j && !ch[j][i]) j = f[j];
                f[u] = ch[j][i];
                if(val[ch[j][i]]) val[u] = 1;
            }
        }
    }
    void ADD(int *A , int *B) //大数加法A=A+B
    {
        int len1 = 0 , len2 = 0;
        for(int i = M-1; i >= 0; i--) if(A[i]) {len1 = i; break;}
        for(int i = M-1; i >= 0; i--) if(B[i]) {len2 = i; break;}
        int len = max(len1 , len2);
        for(int i = 0; i <= len; i++) {
            A[i] += B[i];
            A[i+1] += A[i] / 10;
            A[i] %= 10;
        }
    }
    int dp[N][maxn][M]; //dp[i][j]表示第i步时,到达状态j的字符串数
    void DP(int m , int n)
    {
        memset(dp , 0 , sizeof(dp)); dp[0][0][0] = 1;
        for(int i = 1; i <= m; i++) for(int j = 0; j < sz; j++) {
            if(val[j]) continue;
            for(int k = 0; k < n; k++)
            {
                int cur = ch[j][idx(s[k])];
                if(val[cur]) continue;
                ADD(dp[i][cur] , dp[i-1][j]);
            }
        }
        int ans[M] = {0} , len = 0;
        for(int i = 0; i < sz; i++) if(!val[i]) ADD(ans , dp[m][i]);
        for(int i = M-1; i >= 0; i--) if(ans[i]) {len = i; break;}
        for(int i = len; i >= 0; i--) cout << ans[i];
        cout << endl;
    }
};

AC ac;
int main()
{
    //cout << sizeof(ac.dp) / 1024 << endl;
    int n , m , p;
    while(cin >> n >> m >> p)
    {
        getchar(); ac.init();
        for(int i = 1; i <= p; i++) {
            string str; cin >> str;
            ac.insert(str , i);
        }
        ac.getfail();
        ac.DP(m , n);
    }
}

数据

  • 50 50 10
    qwertyuiop[]\asdfghjkl;’zxcvbnm,./QWERTYUIOP{}|AS
    aegaegu
    etoijqt
    tquqi
    witowwt
    zxcjnc
    oeit
    potieq
    iojge
    nvoq
    piqper
    88816479228348672447914159817057714124274948616722531360571673747292358424682407
    63290

  • 32 50 10
    、¥ウЖ┆q忏溴骁栝觌祉铒?
    Β
    驿?
    馥?
    癌ē?
    く.?
    く.铽
    Β‘飒
    ウq憝?
    驿Γ飒
    隘ルΒ°?
    1721978265757417133897998525905218434633371225724869729302087935758854628748

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值