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. CAST function should support INT synonym for SIGNED. i.e. CAST(y AS INT)

      Login / Register Developer Zone Bugs Home Report a bug Statistics Advanced search Saved searches T ...

  2. 零基础如何学习java更有效呢?

    零基础学java,不知道该如何入手?也不知道学习的方向,很多人会问零基础怎么样学习,有没有什么入门的书籍推荐:只要方法正确,零基础学好java也是有机会的哦. 一.理解Java思想 Java是一门面向 ...

  3. Atitit.ide eclipse编译速度慢的解决

    Atitit.ide eclipse编译速度慢的解决 以为是jar的体积与数量,貌似无关.. Err java的数量,貌似无关 Java的数量,关系不大... 最终原因:show>>pro ...

  4. 开窗函数使用及sql自行构建枚举数据用于关联

    1, SELECT  * FROM    ( SELECT    ROW_NUMBER() OVER ( PARTITION BY process_instance_id (区分相似数据的字段,逗号分 ...

  5. BFC总结

    图1:问题图    图2:代码    图3:解决图 问题图与解决图的区别:黄色箭头那行代码的无和有 BFC 定义 BFC(Block formatting context)直译为"块级格式化 ...

  6. 深入JavaScript:词法分析、连续赋值猜想

    JavaScript:词法分析.连续赋值猜想 原创文章,转摘请注明出处:苏福:http://www.cnblogs.com/susufufu/p/5851642.html 深夜发文,先吐槽下博客园的编 ...

  7. 轻松掌握:JavaScript代理模式、中介者模式

    代理模式.中介者模式 代理模式 在面向对象设计中,有一个单一职责原则,指就一个类(对象.函数)而言,应该仅有一个引起它变化的原因.如果一个对象承担了过多的职责,就意味着它将变得巨大,引起它变化的原因就 ...

  8. 使用setTimeout模拟setInterval效果

    由于现在部分浏览器基于对系统性能的优化,在使用setInterval的时候,在页面没有获得关注的状态,浏览器可以会自动将setInterval终端,等到该页面重新获得关注时再开启.这样就会使得一些基于 ...

  9. iOS NSArray数组过滤

    需求:在一个数组里面,将在这个数组中的并且在另一个数组里面的元素过滤掉. 即:在一个数组dataArray里面,将在dataArray数组中的并且在filteredArray数组里面的元素过滤掉. / ...

  10. iOS之9.3真机适配-Could not find Developer Disk Image问题

    Could not find Developer Disk Image 这是由于真机系统过高或者过低,Xcode中没有匹配的配置包文件,我们可以通过这个路径进入配置包的存放目录: /Applicati ...