POJ 1625 Censored!

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.


【题目分析】
多模板的题目,显然应该用AC自动机,然后计数类的dp。如果trie[ j ][ k ] 不是一个单词的末尾,就可以累加到dp[ i+1 ] [ trie[ j ][ k ] ]上。
最后统计输出。由于压位储存。注意前导零的输出。


【代码】

#include <cstdio>
#include <map>
#include <cstring>
#include <queue>
using namespace std;
int n,m,q,tot=1;
map<char,int>mp;
char s[60];
int tr[210][75],fail[210],v[210];
struct num
{
    int l,a[100];
    num operator + (const num &x) const
    {
        num ans;
        int len;
        memset(ans.a,0,sizeof(ans.a));
        for (int i=1;i<=l||i<=x.l;i++)
        {
            ans.a[i]+=a[i]+x.a[i];
            ans.a[i+1]+=ans.a[i]/10000;
            ans.a[i]%=10000;
        }
        if (l<x.l) len=x.l+1;
        else len=l+1;
        while (!ans.a[len]&&len) len--;
        ans.l=len;
        return ans;
    }
}dp[110][110],ans;
void insert()
{
    scanf("%s",s);
    int now=1,len=strlen(s)-1;
    for (int i=0;i<=len;++i)
    {
        if (!tr[now][mp[s[i]]]) tr[now][mp[s[i]]]=++tot;
        now=tr[now][mp[s[i]]];
    }
    v[now]=true;
}
void bfs()
{
    queue <int> q;
    q.push(1);
    while (!q.empty())
    {
        int x=q.front();q.pop();v[x]|=v[fail[x]];
        for (int i=0;i<n;++i)
        {
            int j=fail[x];
            while (j&&!tr[j][i]) j=fail[j];
            if (tr[x][i]) fail[tr[x][i]]=j?tr[j][i]:1,q.push(tr[x][i]);
            else  tr[x][i]=j?tr[j][i]:1;
        }
    }
}
void dpp()
{
    dp[0][1].a[1]=1; dp[0][1].l=1;
    for (int i=0;i<m;++i)
    {
        for (int j=1;j<=tot;++j)
        {
            for (int k=0;k<n;++k)
            {
                int p=tr[j][k];
                if (v[p]) continue;
                dp[i+1][p]=dp[i+1][p]+dp[i][j];
            }
        }
    }
}
void prt(num x)
{
    printf("%d",x.a[x.l]);
    for (int i=x.l-1;i>=1;i--)
    {
        int y=x.a[i];
        if (y<1000) printf("0");
        if (y<100) printf("0");
        if (y<10) printf("0");
        printf("%d",y);
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&q); scanf("%s",s);
    for (int i=0;i<n;++i) mp[s[i]]=i;
    for (int i=0;i<q;++i) insert();
    bfs(),dpp();
    for (int i=1;i<=tot;++i)
        if (!v[i]) ans=ans+dp[m][i];
    prt(ans);
    printf("\n");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值