题意:给出一个由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. USD词汇表(USD Glossary)

    这篇文章是在学习USD的过程中龟速写成的,目的是将USD的核心设计.相关概念的说明.以及配套API整理出来,为后续进行的USD开发工作提供中文资料支持. 实际上也只有充分理解了USD设计中的每一个知识 ...

  3. 多节点通过PPP连接,节点/用户/客户机之间互相访问ping

    多节点通过PPP连接,节点/用户/客户机之间互相访问ping 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-04-14. 有A, B, C 三台客户机,通过ppp虚拟拨号连接到服务 ...

  4. Spring的IOC原理

    1. IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机 ...

  5. RDLC报表系列--------初级报表

    前面记录下了很多平时开发遇到的问题,RLDC之前也是不会,只会水晶报表,后来也慢慢的也上手了.把这些记录下来,以后用的着 1.打开VS添加新建项,选择Reporting,选择报表,后缀名为RLDC的名 ...

  6. Python 目录整理

    基础部分: 1  常量  2 字符编码  字符编码补充 3 编码集 4 break与continue 5装饰器 6 迭代器 7生成器 8面向过程 9模块 10包 11 递归调用 12文件的用途 13类 ...

  7. zxing生成二维码和条码

    /*** * 生成二维码方法 * @param str 生成内容 * @param widthHeight 宽度和高度 * @return * @throws WriterException */ p ...

  8. react-redux笔记

    用vuex来对比来说明 分类 vuex redux react-redux 写state commit mutation (mutable state) dispatch reducer (immut ...

  9. 64 位 Windows 平台开发注意要点之文件系统重定向

    Program Files 的重定向 很多开发人员都知道,在 64 位 Windows 系统上,32 位程序是无法获取得到 C:\Program Files 的完整路径的,只能获取到 C:\Progr ...

  10. Nginx+Memcache+一致性hash算法 实现页面分布式缓存(转)

    网站响应速度优化包括集群架构中很多方面的瓶颈因素,这里所说的将页面静态化.实现分布式高速缓存就是其中的一个很好的解决方案... 1)先来看看Nginx负载均衡 Nginx负载均衡依赖自带的 ngx_h ...