uva 1401
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing
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.
Sample Input
abcd
4
a
b
cd
ab
Sample Output
Case 1: 2
Nanjing 2007-2008
3942 - Remember
dp[i] = sum(dp[i + len))
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int MAX = 3e5 + ;
const int MOD = ;
const int maxnode = * + ;
int ch[maxnode][];
int val[maxnode];
int sz;
int idx(char c) { return c - 'a';} void insert(char *s, int v) {
int u = , n = strlen(s);
for (int i = ; i < n ; ++i) {
int c = idx(s[i]);
if (!ch[u][c]) {
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
} val[u] = v;
} char str[MAX];
int s;
int dp[MAX]; void solve(int ca) {
memset(dp, , sizeof(dp));
int n = strlen(str);
dp[n] = ;
for (int i = n - ; i >= ; --i) {
int u = ;
for (int j = i; j < n; ++j) {
if (ch[u][ idx(str[j]) ]) {
if (val[ ch[u][ idx(str[j]) ] ] == ) dp[i] = (dp[i] + dp[j + ]) % MOD;
u = ch[u][ idx(str[j]) ];
} else {
break;
}
}
} printf("Case %d: %d\n", ca, dp[]);
} int main()
{
// freopen("sw.in", "r", stdin);
int ca = ;
while (scanf("%s", str) != EOF) {
scanf("%d", &s);
sz = ;
memset(ch[], , sizeof(ch[]));
char ss[];
for (int i = ; i < s; ++i) {
scanf("%s", ss);
insert(ss, );
} solve(ca++); }
//cout << "Hello world!" << endl;
return ;
}
uva 1401的更多相关文章
- UVA 1401 - Remember the Word(Trie+DP)
UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- uva 1401 dp+Trie
http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1401 Remember the Word
字典树优化DP Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVa 1401 (Tire树) Remember the Word
d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) ...
- UVA 1401 Remember the Word(用Trie加速动态规划)
Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...
- LA 3942 && UVa 1401 Remember the Word (Trie + DP)
题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- 大白书 209 remember the word
F - Remember the Word Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Sub ...
随机推荐
- LNMP环境搭建——PHP篇
一.源代码安装 1.编译安装 ./configure --prefix=/usr/local/php\ --with-config-file-path=/usr/local/php/etc --wit ...
- VS2010 使用TeeChart绘图控件 - 之一 - 控件和类的导入
vs2010的用法和vc6有很大的不同,特别是在一些函数调用那里,当然.控件导入也是很不一样的 安装好控件后就可以在工程里加入teechart控件了 加入方法有如下几种: 1.添加Teechart控件 ...
- JSP-Runoob:JSP 动做元素
ylbtech-JSP-Runoob:JSP 动做元素 1.返回顶部 1. JSP 动作元素 与JSP指令元素不同的是,JSP动作元素在请求处理阶段起作用.JSP动作元素是用XML语法写成的. 利用J ...
- [Swift通天遁地]二、表格表单-(2)创建右侧带有索引的UITableView(表单视图)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]七、数据与安全-(11)如何检测应用程序中的内存泄露
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]八、媒体与动画-(7)实现多个动画的顺序播放效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Vue 页面回退参数被当作字符串处理
当时情景是这样的,我从A页面跳到B页面时会传一个Boolean类型的参数,当B跳到C,再从C返回B的时候,控制台打印发现参数还在,可是判断怎么都不起作用,后来发现,当页面返回的时候,默认将参数变成了字 ...
- git配置用户信息
git配置用户信息 一.在本地配置用户信息 配置内容:user.name 和 user.email 配置目的:Git用来记录谁做了什么事 配置方法:windows下打开Git Bash进行操作 gi ...
- Java使用Player播放mp3
大家平时闲了都会听听歌,散散心,于是很多人就问,在Java里边如何播放歌曲呢,唉,别说,在Java里边还真能歌曲,下面我为大家揭晓. 我们都知道Java里边做什么都需要对应的jar包,首先贴上mave ...
- layout 自适应详解
@{ ViewBag.Title = "人员查找"; ViewBag.LeftWidth = "200px"; ViewBag.MiddleW ...