[SCU 4507] 奶牛情书 (AC自动机)

SCU - 4507

求给定长度的文本串,使得每个模式串至少出现过一次
求这样的模式串的个数


AC自动机禁止模式串的裸题
这题求的是一个串至少出现过一次
只要求一次都没出现过的方案总数
再拿所有方案总数相减即可得到答案

然后就拿所有模式串构造 Trie树,然后在上面跑 AC自动机
跑到单词结尾的时候不转移,这样就能求出答案

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) (a*a)

const int MOD=10007,nxt_siz=26,noden=60*100+10;
struct AC_Auto
{
    struct node
    {
        char chr;
        int val,fail,nxt[nxt_siz];
    } *trie;
    int siz;

    AC_Auto(int size){siz=0;trie=new node[size];memset(trie,0,sizeof(node)*size);}
    ~AC_Auto(){delete []trie;}

    int chnum(char x){return x-'A';}
    void addstr(char*);
    void init();
    int solve();
};
int N,M;
int dp[110][noden];

int main()
{
    int T;
    scanf("%d", &T);
    for(int ck=1; ck<=T; ck++)
    {
        AC_Auto ac(noden);
        scanf("%d%d", &N, &M);
        for(int i=0; i<N; i++)
        {
            char patn[110];
            scanf(" %s", patn);
            ac.addstr(patn);
        }
        ac.init();
        printf("%d\n", ac.solve());
    }
    return 0;
}

void AC_Auto::addstr(char *str)
{
    int len=strlen(str),np=0;
    for(int i=0; i<len; i++)
    {
        int ch=chnum(str[i]);
        if(trie[np].nxt[ch]) np=trie[np].nxt[ch];
        else
        {
            trie[np].nxt[ch]=++siz;
            np=siz;
            trie[np].chr=str[i];
        }
        if(i==len-1) trie[np].val=1;
    }
}

void AC_Auto::init()
{
    trie[0].fail = 0;
    queue<int> que;
    for(int i=0; i<nxt_siz; i++)
    {
        int np = trie[0].nxt[i];
        if(np)
        {
            que.push(np);
            trie[np].fail = 0;
        }
    }

    while(!que.empty())
    {
        int np = que.front();
        for(int i=0; i<nxt_siz; i++)
        {
            int nxp = trie[np].nxt[i];
            int fap = trie[np].fail;
            if(!nxp)
            {
                trie[np].nxt[i] = trie[fap].nxt[i];
                continue;
            }
            que.push(nxp);
            trie[nxp].fail = trie[fap].nxt[i];
            fap = trie[nxp].fail;
            if(trie[fap].val) trie[nxp].val = 1;
        }
        que.pop();
    }
}

int AC_Auto::solve()
{
    CLR(dp);
    dp[0][0]=1;
    int res=0,tot=1;
    for(int i=0; i<M; i++)
    {
        tot=tot*26%MOD;
        for(int j=0; j<=siz; j++)
        {
            if(!dp[i][j]) continue;
            for(int k=0; k<26; k++)
            {
                int nxp=trie[j].nxt[k];
                if(trie[nxp].val) continue;
                dp[i+1][nxp]=(dp[i+1][nxp]+dp[i][j])%MOD;
            }
        }
    }
    for(int i=0; i<=siz; i++) res=(res+dp[M][i])%MOD;
    return ((tot-res)%MOD+MOD)%MOD;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值