2017 四川省省赛 Clannad AC自动机+DP

题目传送门


这道题和  Remember the Word 【UVALive - 3942 】题意基本一致,只是让你输入每个前缀的方案数(mod 1e9+7)

UVALive - 3942   这道题可以字典树DP能过,AC自动机(未优化)+DP也能过。

但是这道题数据范围比 3942 这道题大,而且必须要优化才能过。

做法:AC自动机+last优化+DP,DP我就不用讲了(可以见我3942这道题博客:传送门)。

last优化博客:last优化

last优化:减少更多回溯,能够让你一步跳转到你想要的地方。

本题代码:

///#include<bits/stdc++.h>
///#include<unordered_map>
///#include<unordered_set>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>
#define MT(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const double E=2.718281828459;
const ll mod=1e9+7;
const int INF=0x3f3f3f3f;
const int maxn=1e5+10;

char text[100005];
char word[200005];
ll dp[100005];

struct node
{
    node *child[26];
    node *fail;
    node *last;
    int len;
}*root;

void init(node *x)
{
    x->fail=NULL;
    x->last=NULL;
    x->len=0;
    memset(x->child,NULL,sizeof(x->child));
}

void insert_word(char *s)
{
    node *now=root,*temp;
    int x,i;
    for(i=0;s[i];i++)
    {
        x=s[i]-'a';
        if(now->child[x]==NULL)
        {
            temp=new node();
            init(temp);
            now->child[x]=temp;
        }
        now=now->child[x];
    }
    now->len=i;
}

void build_fail()
{
    queue<node *>q;
    node *now,*temp;
    q.push(root);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<=25;i++)
        {
            if(now->child[i])
            {
                if(now==root)
                {
                    now->child[i]->fail=root;
                    now->child[i]->last=root;
                }
                else
                {
                    temp=now->fail;
                    while(temp)
                    {
                        if(temp->child[i])
                        {
                            now->child[i]->fail=temp->child[i];
                            if(temp->child[i]->len)
                                now->child[i]->last=temp->child[i];
                            else
                                now->child[i]->last=temp->child[i]->last;
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(temp==NULL)
                        now->child[i]->fail=root;
                }
                q.push(now->child[i]);
            }
        }
    }
}

void ac_auto(char *s)
{
    int x;
    node *now=root,*temp;
    for(int i=1;s[i];i++)
    {
        x=s[i]-'a';
        while(now!=root&&now->child[x]==NULL)
            now=now->fail;
        now=now->child[x];
        if(now==NULL)
            now=root;
        temp=now;
        while(temp&&temp!=root)
        {
            if(temp->len)
                dp[i]=(dp[i]+dp[i-temp->len])%mod;
            temp=temp->last;
        }
    }
}

int main()
{
    int t,n,m;
    scanf("%d",&t);
    root=new node();
    while(t--)
    {
        init(root);
        scanf("%d %d",&n,&m);
        scanf("%s",text+1);
        for(int i=1;i<=n;i++)
            dp[i]=0;
        dp[0]=1;
        for(int i=1;i<=m;i++)
        {
            scanf("%s",word);
            insert_word(word);
        }
        build_fail();
        ac_auto(text);
        for(int i=1;i<=n;i++)
        {
            printf("%lld",dp[i]);
            printf(i==n?"\n":" ");
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值