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. linq lambda left join

    //var list = table1.Join(table2, ee => ee.Id, ff => ff.table1_Id, (ee, ff) => new { ee, ff  ...

  2. EntityFrame Work 6 Code First 配置字段为varchar 类型

    EntityFrame Work 6 配置字符串属性是否支持Unicode 内容 默认情况下,字符串为Unicode(SQLServer 中的nvarchar).您可以使用IsUnicode 方法指定 ...

  3. 【C#进阶系列】22 CLR寄宿和AppDomain

    关于寄宿和AppDomain 微软开发CLR时,将它实现成包含在一个DLL中的COM服务器. 任何Windows应用程序都能寄宿(容纳)CLR.(简单来讲,就是CLR在一个DLL中,通过引用这个DLL ...

  4. Oracle循环中的EXIT、RETURN、CONTINUE解密

    有时候编写Oracle中用游标等信息去循环处理逻辑的时候,对EXIT.RETURN.CONTINUE很容易搞混淆,网上搜了资料也不是很清楚,所以本人自己写了一小段代码测试了这三种用法.案例代码如下: ...

  5. css中的各种单位简述以及ios10下safari禁止缩放的问题

    px:绝对单位,页面按精确像素展示 em:相对单位,基准点为父节点字体的大小,如果自身定义了font-size按自身来计算(浏览器默认字体是16px),整个页面内1em不是一个固定的值. rem:相对 ...

  6. 使用NuGet助您玩转代码生成数据————Entity Framework 之 Code First

    [前言] 如果是Code First老鸟或者对Entity Framework不感兴趣,就不用浪费时间往下看了. 记得09年第一次接触ORM————Linq2Sql,从此对她的爱便一发不可收拾,一年后 ...

  7. Android UI ListView的使用

    一.ListView的理解  1.什么ListView?   一种用来显示多个可滑动项(Item)列表的的ViewGroup 需要使用Adapter将集合数据和每一个Item所对应的布局动态适配到Li ...

  8. 深度技术GHOST WIN7系统32.64位j极速安装版 V2016年

    系统来自系统妈:http://www.xitongma.com 深度技术GHOST win7系统64位j极速安装版 V2016年3月 系统概述 深度技术ghost win7系统64位j极速安装版  版 ...

  9. 动态计算Label高度

    //1.设置该label的numberOfLines为0 self.titleLabel.numberOfLines = 0;    //2.字体的设置要与之前相同 NSDictionary * at ...

  10. 【Lucene】三个高亮显示模块的简单示例-Highlighter

    Lucene针对高亮显示功能提供了两种实现方式,分别是Highlighter和FastVectorHighlighter 这里的三个示例都是使用Highlighter: 示例代码: package c ...