[JSOI2007] 文本生成器

91 篇文章 1 订阅
5 篇文章 0 订阅

题目描述:

qwq.

题目分析:

惊了,居然可以在AC自动鸡上跑DP(大雾
直接算好像比较困难,所以考虑先算不可读的串的个数,再拿总串数去减。
不可读的串的数量就是在AC自动机上走M步而不经过结尾节点(包括结尾点和fail指向结尾点的节点)的路径条数。
f[i][j]表示路径长度为i在自动鸡上j节点的方案数
那么f[i][j]可以转移f[i+1][son[j][k]]
最后算一下补集即可

题目链接:

Luogu 4052
BZOJ 1030

Ac 代码:

#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
#define mod 10007
std::string s1;
std::queue <int> dl;
struct node{
    int fail,end;
    int ch[70*100+100];
}st[20001];
int f[105][6005];
int sz;
int n,m;
inline void build(std::string s)
{
    int len=s.length();
    int now=0;
    for(int i=0;i<len;i++)
    {
        if(!st[now].ch[s[i]-'A']) st[now].ch[s[i]-'A']=++sz;
        now=st[now].ch[s[i]-'A'];
    }
    st[now].end=1;
}
inline int fastpow(int x,int y)
{
    int ans=1;
    for(;y;x=(x*x)%mod,y>>=1)
     if(y&1) ans=(ans*x)%mod;
    return ans;
}
inline void make_fail()
{
    for(int i=0;i<26;i++)
    if(st[0].ch[i]) dl.push(st[0].ch[i]);
    while(!dl.empty())
    {
        int now=dl.front();
        dl.pop();
        for(int i=0;i<26;i++)
        {
            if(st[now].ch[i])
            {
                st[st[now].ch[i]].fail=st[st[now].fail].ch[i];
                dl.push(st[now].ch[i]);
            }
            else st[now].ch[i]=st[st[now].fail].ch[i];

        }
        st[now].end|=st[st[now].fail].end;
    }
}
inline int dp()
{
    f[0][0]=1;
    for(int i=1;i<=m;i++)
    for(int j=0;j<=sz;j++)
    if(!st[j].end)
    {
        for(int k=0;k<26;k++)
         f[i][st[j].ch[k]]=(f[i][st[j].ch[k]]+f[i-1][j])%mod;
    }
    int ans=0;
    for(int i=0;i<=sz;i++) if(!st[i].end) ans=(ans+f[m][i])%mod;
    int res=fastpow(26,m);
    return ((res-ans)+mod)%mod;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        std::cin>>s1;
        build(s1);
    }
    make_fail();
    printf("%d\n",dp());
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值