题意:给出一个由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. JavaWeb数据库长时间不访问断开链接解决思路

    这几天开发的线上商超系统长时间不操作,会频繁的出现第一次登陆或者跟数据库操作有关的方法都会报500错误,很是鸡肋啊这个问题. 经过网上不断的探索,在知识的海洋里畅游了几分钟后我自己总结出一套方法,我用 ...

  2. 在图像中随机更改像素值程序——matlab

    I=imread('C:\Users\wangd\Desktop\result3.png'); % m = rgb2gray(I); % r = unidrnd(,,); %产生一个1*100的数组, ...

  3. Kong(V1.0.2)loadbalancing

    介绍 Kong为多个后端服务提供了多种负载平衡请求的方法:一种简单的基于DNS-based的方法,以及一种更动态的环形负载均衡器ring-balancer,它还允许在不需要DNS服务器的情况下使用se ...

  4. (最详细)小米Note 2的usb调试模式在哪里开启的教程

    当我们使用安卓手机链接Pc的时候,或者使用的有些工具比如我们学院营销团队当使用的工具引号精灵,之前使用的老版本就需要开启USB开发者调试模式下使用,现当新版本不需要了,如果手机没有开启USB开发者调试 ...

  5. rediscluster安装

    Redis 3.2.1集群搭建   一.概述 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2) ...

  6. Jmeter发送邮件功能SMTP Sampler

    介绍Jmeter的发送邮件功能,使用的Sampler是SMTP Sampler,详细说明每个配置项的功能 从上往下介绍需要用到的配置项: Server settings Server: 服务器地址 P ...

  7. OkHttp之Interceptor

    先看RealCall 发送一个请求,我们会先创建一个request,然后使用okHttpClient.newCall(request),创建一个call进行网络请求,这个call,就是RealCall ...

  8. spring框架篇(一)-------spring简介与配置文件使用控制反转事例

    spring简介 Spring 是一个开源框架,中文意思就是春天,也许是作者想让自己的这个框架给Java开发人员带来春天吧.其官方网站是 https://spring.io/ ,可以在官方网站下载到完 ...

  9. ssh 公钥无秘登录问题

    1. 验证服务启动,网络端口连接正常 可以使用nc,telnet,或者密码模式的ssh来验证 2. 验证ssh client端的配置正确 可以尝试登录另外一台主机, 或者本机自校验 3. 验证ssh ...

  10. ECMAScript 6 新特性-set。const

    一.let命令是es6新增的特性,作用与var命令类似,声明变量,不同之处在于声明的变量的作用域为块级作用域.引入let后带来了很多新的特性. 1作用域,es5之前之后函数作用域和全局作用域,let的 ...