UVALive - 3942

Neal is very curious about combinatorial problems, and now here comes a problem about words. Know- ing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.

Since Jiejie can’t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie’s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.

The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.

Input

The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.

The second line contains an integer S, 1 ≤ S ≤ 4000.

Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.

There is a blank line between consecutive test cases. You should proceed to the end of file.

Output

For each test case, output the number, as described above, from the task description modulo 20071027.


白书,字符串用单词分割


DP,d[i]表示i到结尾的方案数

d[i]=sum{d[i+len(x)]|x是s[i]开始的前缀}

暴力的话枚举每个单词代价4000

可以用Trie找前缀代价100

//
// main.cpp
// la3942
//
// Created by Candy on 10/14/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int W=3e5+,S=4e3+,N=,MOD=;
int n;
char s[W];
char a[S][N];
int t[S*N][],sz=,val[S*N];
void init(){
memset(t[],,sizeof(t[]));
}
void insert(char ch[]){
int len=strlen(ch+),u=;
for(int i=;i<=len;i++){
int c=ch[i]-'a';
if(!t[u][c]){
t[u][c]=++sz;
memset(t[sz],,sizeof(t[sz]));
val[sz]=;
}
u=t[u][c];
}
val[u]=;
}
int d[W];
void dp(){
memset(d,,sizeof(d));
int n=strlen(s+);
d[n+]=;
for(int i=n;i>=;i--){
int u=;
for(int j=i;j<=n&&j<=i+;j++){
int c=s[j]-'a';
if(!t[u][c]) break;
u=t[u][c];
if(val[u]) d[i]=(d[i]+d[j+])%MOD;
}
}
}
int main(int argc, const char * argv[]) {
int cas=;
while(scanf("%s%d",s+,&n)!=EOF){
init();
for(int i=;i<=n;i++){
scanf("%s",a[i]+);
insert(a[i]);
}
dp();
printf("Case %d: %d\n",++cas,d[]);
}
return ;
}

UVALive - 3942 Remember the Word[Trie DP]的更多相关文章

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

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

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

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

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

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

  4. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

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

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

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

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

  7. (Trie) uvalive 3942 Remember the word

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

  8. LA 3942 && UVa 1401 Remember the Word (Trie + DP)

    题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...

  9. UVA 1401 - Remember the Word(Trie+DP)

    UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...

随机推荐

  1. Java之数组篇

    动手动脑,第六次Tutorial--数组 这次的Tutorial讲解了Java中如何进行数组操作,包括数组声明创建使用和赋值运算,写这篇文章的目的就是通过实际运用已达到对数组使用的更加熟练,下面是实践 ...

  2. UDS(ISO14229-2006) 汉译(No.6 应用层服务)

    6.1总览 应用层服务通常被当作诊断服务.应用层服务用于在基于客户端-服务器的系统(Client-Server base System)中执行一些功能,例如针对车载服务器(ECU)的检测.检查.监控和 ...

  3. 10、ASP.NET MVC入门到精通——Model(模型)和验证

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 模型就是处理业务,想要保存.创建.更新.删除的对象. 注解(通过特性实现) DisplayName Required StringLengt ...

  4. iOS RunLoop简介

    一.什么是RunLoop? RunLoop是运行循环,每个Cocoa应用程序都由一个处于阻塞状态的do/while循环驱动,当有事件发生时,就把事件分派给合适的监听器,如此反复直到循环停止.处理分派的 ...

  5. 安装Portal for ArcGIS时如何正确配置HTTPS证书

    SSL协议位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持.SSL协议可分为两层: SSL记录协议(SSL Record Protocol):它建立在可靠的传输协议(如TCP)之上,为 ...

  6. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q121-Q123)

    Question  121 You are designing a SharePoint 2010 workflow that will be used to monitor invoices. Th ...

  7. eclipse配置tomcat 和JRE环境

    配置JRE环境,通俗点讲就是添加一个不同版本的jdk window——preferences——java——installed jres 点击add添加   选择standard VM 选择一个本机正 ...

  8. idea使用优化常用快捷键设置

    1. 界面美观 1.1. 主题 1.2. 改Project面板大小 2. 快捷键 2.1智能提示 2.2常用快捷键 3.编辑器调整 3.1 代码提示忽略大小写 3.2 显示行号 3.3 优化自动导入 ...

  9. Mac使用极简教程

    最近领导让我写一篇关于Mac的使用教程,因为使用人群未知,所以尽量写的通俗易懂,可谓是关于Mac电脑使用的精简教程吧,在此发表出来以供参考. Mac因为安全性而闻名,我们拥有了一部Mac,那么我们来了 ...

  10. Openstack api 学习文档 & restclient使用文档

    Openstack api 学习文档 & restclient使用文档 转载请注明http://www.cnblogs.com/juandx/p/4943409.html 这篇文档总结一下我初 ...