AC自动机+DP+大数poj1625

本文探讨了一个算法问题,即在一个特定的字母表中找出不含禁用词汇的所有可能字符串组合。通过构建AC自动机并结合矩阵运算,文章提供了一种高效解决该问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Language:
Censored!
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 8102 Accepted: 2191

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
这个题跟2778一样,但不知道问世么不能用矩阵,网上说会超时,但好像数据不会超啊???

dp[i][j]表示第i步在第j个节点的方法数。flag[k]=0表示不是非法节点,son是k的儿子节点的集合。

  dp[i][j]=sum(dp[i-1][k]*mat[k][j]&&val[ch[k][j]]==0);

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=110;
int SIGMA_SIZE;
int n,m,p;
char P[60],s[60];
map<char,int> mp;

struct Matrix
{
    int mat[maxn][maxn];
    Matrix(){memset(mat,0,sizeof(mat));}

};
struct AC
{
    int ch[maxn][256],val[maxn];
    int fail[maxn],last[maxn];
    int sz;
    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}
    int idx(char x){return mp[x];}
    void insert(char *s)
    {
        int n=strlen(s);
        int u=0;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=1;
    }
    void getfail()
    {
        queue<int> q;
        fail[0]=0;
        int u=0;
        for(int i=0;i<SIGMA_SIZE;i++)
        {
            u=ch[0][i];
            if(u){fail[u]=last[u]=0;q.push(u);}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            if(val[fail[r]])val[r]=1;
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                u=ch[r][c];
                if(!u){ch[r][c]=ch[fail[r]][c];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][c])v=fail[v];
                fail[u]=ch[v][c];
                last[u]=val[fail[u]]?fail[u]:last[fail[u]];
            }
        }
    }
    Matrix getMatrix()
    {
        Matrix res;
        for(int i=0;i<sz;i++)
            for(int j=0;j<SIGMA_SIZE;j++)
                if(!val[ch[i][j]])res.mat[i][ch[i][j]]++;
        return res;
    }
}ac;
/*
 * 高精度,支持乘法和加法
 */
struct BigInt
{
    const static int mod = 10000;
    const static int DLEN = 4;
    int a[600],len;
    BigInt()
    {
        memset(a,0,sizeof(a));
        len = 1;
    }
    BigInt(int v)
    {
        memset(a,0,sizeof(a));
        len = 0;
        do
        {
            a[len++] = v%mod;
            v /= mod;
        }while(v);
    }
    BigInt(const char s[])
    {
        memset(a,0,sizeof(a));
        int L = strlen(s);
        len = L/DLEN;
        if(L%DLEN)len++;
        int index = 0;
        for(int i = L-1;i >= 0;i -= DLEN)
        {
            int t = 0;
            int k = i - DLEN + 1;
            if(k < 0)k = 0;
            for(int j = k;j <= i;j++)
                t = t*10 + s[j] - '0';
            a[index++] = t;
        }
    }
    BigInt operator +(const BigInt &b)const
    {
        BigInt res;
        res.len = max(len,b.len);
        for(int i = 0;i <= res.len;i++)
            res.a[i] = 0;
        for(int i = 0;i < res.len;i++)
        {
            res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0);
            res.a[i+1] += res.a[i]/mod;
            res.a[i] %= mod;
        }
        if(res.a[res.len] > 0)res.len++;
        return res;
    }
    BigInt operator *(const BigInt &b)const
    {
        BigInt res;
        for(int i = 0; i < len;i++)
        {
            int up = 0;
            for(int j = 0;j < b.len;j++)
            {
                int temp = a[i]*b.a[j] + res.a[i+j] + up;
                res.a[i+j] = temp%mod;
                up = temp/mod;
            }
            if(up != 0)
                res.a[i + b.len] = up;
        }
        res.len = len + b.len;
        while(res.a[res.len - 1] == 0 &&res.len > 1)res.len--;
        return res;
    }
    void output()
    {
        printf("%d",a[len-1]);
        for(int i = len-2;i >=0 ;i--)
            printf("%04d",a[i]);
        printf("\n");
    }
};
BigInt dp[2][maxn];
int main()
{
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
    {
        scanf("%s",P);
        ac.clear();
        mp.clear();
        for(int i=0;i<strlen(P);i++)
            mp[P[i]]=i;
        SIGMA_SIZE=n;
        for(int i=0;i<p;i++)
        {
            scanf("%s",s);
            ac.insert(s);
        }
        ac.getfail();
        Matrix A=ac.getMatrix();
        int now=0;
        dp[now][0]=1;
        for(int i=1;i<ac.sz;i++)dp[now][i]=0;
        for(int i=1;i<=m;i++)
        {
            now^=1;
            for(int j=0;j<ac.sz;j++)dp[now][j]=0;
            for(int j=0;j<ac.sz;j++)
                for(int k=0;k<ac.sz;k++)
                if(A.mat[j][k]>0)dp[now][k]=dp[now][k]+dp[now^1][j]*A.mat[j][k];
        }
        BigInt ans=0;
        for(int i=0;i<ac.sz;i++)
            ans=ans+dp[now][i];
        ans.output();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值