HDU 1625 Censored! (AC自动机 + 大数 + dp)

57 篇文章 0 订阅
15 篇文章 0 订阅

题意:

给定你n 种字符, 和p 个禁止串, 问你长度为m 的串有多少种 不包含 禁止串?

思路:

AC自动机是显然的。

矩阵快速幂也可以 ,dp 也可以。

但是这是个大数。  极限答案有85位左右。

我用快速幂写了一发,不是爆内存就是因为内存开的过大 小样例都出不来。

只能考虑dp了。

dp 状态很好想:

令dp[i][j] 表示目前走 第i 位长度, 在自动机上j 节点的方案数。


转移就是上一个节点 转下一个节点了。

dp[i][j] += dp[i-1][k]

为了节省空间  搞成滚动数组即可

不过看了看讨论区, 说是 字符串有负数出现 就是 输入 的ASCII码会在128~255之间? 但是我的并没有考虑这种情况, 也A了= =(maybe数据改掉了?)


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



///高精度
const int MAXN = 90;
struct bign
{
    int len, s[MAXN];
    bign ()
    {
        memset(s, 0, sizeof(s));
        len = 1;
    }
    bign (int num) { *this = num; }
    bign (const char *num) { *this = num; }
    bign operator = (const int num)
    {
        char s[MAXN];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    bign operator = (const char *num)
    {
        for(int i = 0; num[i] == '0'; num++) ;  //去前导0
        len = strlen(num);
        for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
        return *this;
    }
    bign operator + (const bign &b) const //+
    {
        bign c;
        c.len = 0;
        for(int i = 0, g = 0; g || i < max(len, b.len); i++)
        {
            int x = g;
            if(i < len) x += s[i];
            if(i < b.len) x += b.s[i];
            c.s[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    bign operator += (const bign &b)
    {
        *this = *this + b;
        return *this;
    }
    void clean()
    {
        while(len > 1 && !s[len-1]) len--;
    }


    string str() const
    {
        string res = "";
        for(int i = 0; i < len; i++) res = char(s[i]+'0') + res;
        return res;
    }
};

istream& operator >> (istream &in, bign &x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream& operator << (ostream &out, const bign &x)
{
    out << x.str();
    return out;
}



///







const int maxn = 100 + 3;
char s[100];
int mp[128 ];
int fmp[128];

bign dp[2][maxn];
const int inf = 0x3f3f3f3f;


struct Trie{
    int L, root;
    int next[maxn][50];
    int fail[maxn];
    int flag[maxn];
    int Hash[maxn];
    int fHash[maxn];
    int cnt;
    int mx;

    void init(int mx_){
        L = 0;
        mx = mx_;
        root = newnode();
        cnt = 0;

    }

    int newnode(){
        for (int i = 0; i < mx; ++i){
            next[L][i] = -1;
        }
        flag[L] = 0;
        return L++;
    }

    void insert(char* s){
        int len = strlen(s);
        int nod = root;
        for (int i = 0; i < len; ++i){
            int id = fmp[s[i] ];
            if (next[nod][id] == -1){

                next[nod][id] = newnode();
            }
            nod = next[nod][id];
        }
        flag[nod] = 1;
    }

    void bfs(){
        fail[root] = root;
        queue<int>q;
        for (int i = 0; i < mx; ++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 u = q.front(); q.pop();
            for (int i = 0; i < mx; ++i){
                if (next[u][i] == -1){
                    next[u][i] = next[fail[u] ][i];
                }
                else {
                    fail[next[u][i] ] = next[fail[u] ][i];
                    q.push(next[u][i]);
                }
            }
        }
    }

    void deal(){
        for (int i = 0; i < L; ++i){
            if (flag[i]) continue;
            int tmp = i;
            while(tmp != root){
                if (flag[tmp]){
                    flag[i] = 1;
                    break;
                }
                tmp = fail[tmp];
            }
            if (!flag[i]){
                Hash[cnt++] = i;
                fHash[i] = cnt - 1;
            }
        }
    }


    void solve(int n){
        for (int j = 0; j < maxn; ++j){
            dp[0][j] = 0;
        }
        dp[0][0] = 1;
        int foo = 0;
        for (int k = 1; k <= n; ++k){
            int first = foo;
            int second = foo ^ 1;
            for (int i = 0; i < maxn; ++i){
                dp[second][i] = 0;
            }
            for (int i = 0; i < cnt; ++i){
                for (int j = 0; j < mx; ++j){
                    int now = Hash[i];
                    int nx = next[now][j];
                    if (flag[nx]) continue;
                    dp[second][nx] += dp[first][now];
                }
            }
            foo ^= 1;
        }
        bign ans = 0;
        for (int i = 0; i < cnt; ++i){
            int nod = Hash[i];
            ans += dp[foo][nod];
        }
        string tt = ans.str();
        if (tt.length() == 0)cout<<0<<endl;
        else cout << ans << endl;
    }


}ac;
/**
8881784197001252323389053344726562500000000000000000000000000000000000000000000000000
**/
int main(){
    int n, m, p;
    scanf("%d %d %d",&n, &m, &p);
    scanf("%s", s);

    int len = strlen(s);

    for (int i = 0; i < len; ++i){
        mp[i] = s[i];
        fmp[s[i]] = i;
    }
    ac.init(len);
    for (int i = 0; i < p; ++i){
        scanf("%s", s);
        ac.insert(s);
    }
    ac.bfs();
    ac.deal();
    ac.solve(m);
    return 0;
}

Censored!
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 10370 Accepted: 2841

Description

The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences. 

But after recent election of Mr. Grass Jr. as Freeland president some words offending him were declared unprintable and all sentences containing at least one of them were forbidden. The sentence S contains a word W if W is a substring of S i.e. exists such k >= 1 that S[k] = W[1], S[k+1] = W[2], ...,S[k+len(W)-1] = W[len(W)], where k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a forbidden sentence is to be put to jail for 10 years. 

Find out how many different sentences can be used now by freelanders without risk to be put to jail for using it. 

Input

The first line of the input file contains three integer numbers: N -- the number of letters in Freish alphabet, M -- the length of all Freish sentences and P -- the number of forbidden words (1 <= N <= 50, 1 <= M <= 50, 0 <= P <= 10). 

The second line contains exactly N different characters -- the letters of the Freish alphabet (all with ASCII code greater than 32). 

The following P lines contain forbidden words, each not longer than min(M, 10) characters, all containing only letters of Freish alphabet. 

Output

Output the only integer number -- the number of different sentences freelanders can safely use.

Sample Input

2 3 1
ab
bb

Sample Output

5

Source

Northeastern Europe 2001, Northern Subregion

[Submit]   [Go Back]   [Status]   [Discuss]


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值