SPOJ - GEN Text Generator【AC自动机+矩阵DP】

Time limit 1386 ms
Memory limit 1572864 kB

LoadingTime has been given a task these days. He is required to write a tool called Text Generator. This software is widely used among the kids who are under seven. It generates an article with the size of a given number L for users. If an article contains at least one word which the users know, we consider it readable. Now, LoadingTime wants to know, how many readable articles can it generates, and he can improve his Text Generator. Could you help him??

Input

The input contains multiple test cases.

The first line of each test case contains two integer N (1 <= N <= 10), L (1 <= L <= 1000000). The following N lines contain N words representing the words knew by the users. All the words and the generated article only contain uppercase letters, and the length of each word is not greater than 6.

Output

For each test case, your program should output a integer as LoadingTime required. As the number could be quite large, you only need to print the answer modulo 10007.


题意

给定n(n<=10)个字符串(长度<=6),要求构造一个长度为L(<=10^6)的字符串,至少包含n个字符串中任何一个,求方案数(所有字符都是大写字母)


题目分析

算是 洛谷P4052 [JSOI2007]文本生成器 的加强版吧,建议先做这题

先把问题转化为 可能的字符串总数( 2 6 L 26^L 26L) - 不合法的字符串数
用给定的字符串构造AC自动机,我们需要求从AC自动机根开始走L步不经过单词结点的方案数

上面那道题的dp方法肯定不行了
考虑构造这样一个矩阵G, G [ i ] [ j ] G[i][j] G[i][j]表示AC自动机结点 i i i可以一步到达结点 j j j的路径数(i,j都不是单词结点)
对这个矩阵做矩阵乘法, G L [ i ] [ j ] G^L[i][j] GL[i][j]即为结点 i i i可以L步到达结点 j j j的路径数
所以不合法的方案数为 a n s = ∑ i = 0 s z G L [ 0 ] [ i ] ans=\sum_{i=0}^{sz} G^L[0][i] ans=i=0szGL[0][i]
矩阵快速幂优化即可

这题十分毒瘤卡常,单词结点要去掉再给剩下的节点重新编号
注意开始数组尽量恰着开,多组数据初始化可以省时间
取膜要在大于等于mod时再进行

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long lt;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int mod=10007;
const int maxn=62;
int n,L;
int ch[maxn][26],rem[maxn];
int fail[maxn],pos[maxn],tot,sz;
char pt[maxn];
struct node{ 
    int a[maxn][maxn]; 
    node(){memset(a,0,sizeof(a));}
}G;

void init()
{
    sz=tot=0;
    memset(ch,0,sizeof(ch));
    memset(rem,0,sizeof(rem));
    memset(G.a,0,sizeof(G.a));
}

int qpow(int a,int k)
{
    int res=1;
    while(k>0){
        if(k&1) res*=a;
        a*=a; k>>=1;
        if(res>=mod) res%=mod;
        if(a>=mod) a%=mod;
    }
    return res;
}

void ins(char* ss)
{
    int u=0;
    for(int i=0;ss[i];++i)
    {
        int x=ss[i]-'A';
        if(!ch[u][x]) ch[u][x]=++sz;
        u=ch[u][x];
    }
    rem[u]=1;
}

void ACM()
{
    queue<int> q;
    for(int i=0;i<26;++i)
    if(ch[0][i]) fail[ch[0][i]]=0,q.push(ch[0][i]);
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=0;i<26;++i)
        {
            if(!ch[u][i]) ch[u][i]=ch[fail[u]][i];
            else{
                fail[ch[u][i]]=ch[fail[u]][i];
                q.push(ch[u][i]);
                rem[ch[u][i]]|=rem[fail[ch[u][i]]];
            }
        }
    }
}

void build()
{
    for(int i=0;i<=sz;++i)
    if(!rem[i]) pos[i]=++tot;
    
    for(int i=0;i<=sz;++i)
    if(!rem[i])
    for(int j=0;j<26;++j)
    if(!rem[ch[i][j]])
    G.a[pos[i]][pos[ch[i][j]]]++;
}

node mul(node x,node y)
{
    node res;
    for(int i=1;i<=tot;++i)
    for(int j=1;j<=tot;++j)
    for(int k=1;k<=tot;++k)
    {
        res.a[i][j]+=x.a[i][k]*y.a[k][j];
        if(res.a[i][j]>=mod) res.a[i][j]%=mod;
    }
    return res;
}

node qpow_mat(node a,int k)
{
    node res;
    for(int i=1;i<=tot;++i) res.a[i][i]=1;
    while(k>0){
        if(k&1) res=mul(res,a);
        a=mul(a,a); k>>=1;
    }
    return res;
}

int main()
{
    while(scanf("%d%d",&n,&L)!=EOF)
    {
    	init();
        for(int i=1;i<=n;++i)
    	{
    		scanf("%s",&pt);
    		ins(pt);
        }
        
        ACM(); build();
        node res=qpow_mat(G,L);
        
        int ans=qpow(26,L),sum=0;
        for(int i=1;i<=tot;++i)
        {
            sum+=res.a[1][i];
            if(sum>=mod) sum%=mod;
        }
        
        ans-=sum;
        if(ans<0) ans+=mod;
        printf("%d\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值