题意:给出一个由S个不同单词组成的字典和一个长字符串。把这个字符串分解成若干个单词的连接(单词可以重复

    使用),有多少种方法?

Sample Input

abcd

4

a

b

cd

ab

Sample Output

Case 1: 2

思路:利用字典树上的dp,dp[i]表示从i到末尾有多少种方案,大白书字典树的例题,虽然是例题还是看了一会儿orz,

    我太弱了,注意ch数组要开成最大节点数*26的大小,表示每个节点连的字母是什么,初始节点为0,val也要开

    成最大节点数的大小,表示该节点是否为单词的最后一个字母。

    然后就是dp的过程了,从末尾往前,每次循环从i到结尾,如果碰到某个单词的结尾(也就是val[j]==1)就代表

    该单词可以作为某些组合的一部分,dp[i]+=dp[j+1],关键部分就是这里了吧。

代码:

#include<iostream>
#include<string.h>
using namespace std;

const int maxn=4001*101;
const int maxm=3e5+5;
const int mod=20071027;

struct Trie{
    int ch[maxn][26];
    int val[maxn];
    int sz;
    Trie(){
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));
    }
    int idx(char c){
        return c-'a';
    }
    void insert(char *s,int v){
        int u=0,l=strlen(s);
        for(int i=0;i<l;i++){
            int x=idx(s[i]);
            if(ch[u][x]==0){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][x]=sz++;
            }
            u=ch[u][x];
        }
        val[u]=v;
    }
}e;

char s[maxm];
char a[110];
long long dp[maxm];

int main(){
    int t=0;
    while(~scanf("%s",s)){
        int n;
        scanf("%d",&n);
        memset(e.ch[0],0,sizeof(e.ch[0]));
        e.sz=1;
        for(int i=0;i<n;i++){
            scanf("%s",a);
            e.insert(a,1);
        }
        int l=strlen(s);
        memset(dp,0,sizeof(dp));
        dp[l]=1;
        int u;
        for(int i=l-1;i>=0;i--){
            u=0;
            for(int j=i;j<l;j++){
                int x=e.idx(s[j]);
                if(e.ch[u][x]==0)break;
                u=e.ch[u][x];
                if(e.val[u])dp[i]+=dp[j+1];
            }
            dp[i]%=mod;
        }
        //printf("%d\n",e.sz);
        //for(int i=0;i<=l;i++)printf("%lld ",dp[i]);
        printf("Case %d: %lld\n",++t,dp[0]);
    }
    return 0;
}

UVALive 3942 Remember the Word的更多相关文章

  1. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  2. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  3. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  4. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  5. UVALive 3942 Remember The Word (Tire)

    状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...

  6. UVALive - 3942 Remember the Word (Trie + DP)

    题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...

  7. UVALive 3942 Remember the Word(字典树+DP)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. (Trie) uvalive 3942 Remember the word

    题意:告诉你一个母串和子串,能用多少种不同的方案组合出母串. 思路:字典树(显然)+DP DP: dp[i]+=dp[j+1]  i<=j<=m-1,且i到j的字符串能在字典树中找到.也就 ...

  9. LA 3942 Remember the Word(前缀树&树上DP)

    3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...

随机推荐

  1. Python2.X和Python3.X的w7同时安装使用

    一.介绍 Python([ˈpaɪθən])是一种面向对象.解释型计算机程序设计语言.Python语法简洁.清晰,具有丰富和强大的类库. Python源代码遵循GPL(GNU General Publ ...

  2. USB协议学习

    URB:USB 请求块(USB request block,urb)是USB 设备驱动中用来描述与USB 设备通信所用的基本载体和核心数据结构,非常类似于网络设备驱动中的sk_buff 结构体.

  3. 京东饭粒捡漏V1.15

    20181105 更新 V1.151.部分BUG修复: 功能介绍1.京东商城专用,支持饭粒模式下单,自己获得京豆返利 2.捡漏模式:帮助用户监控抢购商品,有库存的时候进行抢单,主要是通过添加商品ID ...

  4. WIN10X64_LTSB2016极限精简版by双心

    WIN10X64LTSB2016极限精简版by双心http://www.cnblogs.com/liuzhaoyzz/p/9162113.html 一.前言:关于极限精简版的说明 本系统为极限精简版, ...

  5. final link failed: Nonrepresentable section on output

    编译live555的时候遇到了这个问题,前面的编译没有问题,是在链接的时候出现的,在网上搜索说是缺少 libstdc++ 库.于是,安装之 #sudo apt-get install  libstdc ...

  6. 微服务与Spring Cloud资料

    Microservices Using Spring Boot and Spring Cloud 微服务注册中心 Eureka 架构深入解读 50+ 顶级开源 Kubernetes 工具列表 Apol ...

  7. Linux系统编程——Daemon进程

    目录 Daemon进程介绍 前提知识 Daemon进程的编程规则 Daemon进程介绍 Daemon运行在后台也称作"后台服务进程". 它是没有控制终端与之相连的进程.它独立与控制 ...

  8. 一个故事带你理解if __name__ == '__main__'

    如果你刚刚接触python,相信会在看别人的程序的时候会遇到if __name__ == '__main__'酱紫的语法,如果当时没看懂现在也一知半解的话,看下去,本文可以帮你解决这个问题. 大家都知 ...

  9. Android中刷新Invalidate和postInvalidate的区别

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用.Android提供了Inval ...

  10. 所有设计复杂的ORM都是浮云

    很久没有写文章了. 一直很忙,不是很有时间整理. 今天主要是来吐槽下那些设计很复杂的ORM的. 项目做的越多,越觉得ORM这个东西设计的太复杂实在是没什么意义. 比较推崇Dapper这样比较简单,效率 ...