UVA1401 Remember the word DP+Trie

问题描述

洛谷(有翻译)


题解

DP,设\(opt_i\)代表前\(i\)个字符方案数。

Trie优化,刷表法。


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-'){
        fh=-1;ch=getchar();
    }
    else fh=1;
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    x*=fh;
}

string s,t;
int n,opt[301000],m;

int ch[400003][26],tot,root=1;
int ed[400003];

const int mod=20071027;

int chk(char s){
    return s-'a'+1;
}

void insert(){
    int len=t.size(),p=root;
    for(int i=0;i<len;i++){
        int k=chk(t[i]);
        if(!ch[p][k]) ch[p][k]=++tot;
        p=ch[p][k];
    }
    ++ed[p];
}

void query(int x){
    int p=root,y=x+1,cnt=0;
    while(p){
        if(ed[p]){
            opt[x+cnt]=(opt[x]+opt[x+cnt])%mod;
        }
        int k=chk(s[x+cnt]);
        ++cnt;p=ch[p][k];
    }
}

int testcase;

void solve(){
    memset(opt,0,sizeof(opt));opt[0]=1;
    memset(ch,0,sizeof(ch));tot=1;memset(ed,0,sizeof(ed));
    cin>>m;
    for(int i=1;i<=m;i++){
        cin>>t;
        insert();
    }
    for(int i=0;i<s.size();i++){
        query(i);
    }
    cout<<"Case "<<testcase<<": "<<opt[s.size()]<<endl;
}

int main(){
    ios::sync_with_stdio(false);
    while(1){
        s="";cin>>s;++testcase;
        if(!s.size()) break;
        solve();
    }
    return 0;
}

转载于:https://www.cnblogs.com/liubainian/p/11588320.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值