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 ...
随机推荐
- LeetCode 645. Set Mismatch (集合不匹配)
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- 安装ubuntu远程桌面xrdp可视化设置界面
ubuntu 远程桌面的时候须要从系统-首选项-远程桌面 可是有的ubuntu远程桌面的应用须要自己安装.例如以下是安装命令: sudo apt-get install xrdp
- hdu 2094 产生冠军(拓扑排序)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- Parallel and Perpendicular
题目链接 题意: 输入n,求正n边形中的对角线1和对角线2的个数(对角线1:至少与其它一个对角线平行:对角线2:至少与其它一个对角线垂直).对角线不能是多边形的边 (4 ≤ n ≤ 10e5) 分析: ...
- Bing Maps进阶系列八:在Bing Maps中集成OpenStreetMap地图
Bing Maps进阶系列八:在Bing Maps中集成OpenStreetMap地图 OSM(OpenStreetMap-开放街道地图)服务就是一种发布自己地图数据图片为服务的一种实现类型,开放街道 ...
- Java Swing Action 动作
Swing包提供了一种非常实用的机制来封装命令,并将它们连接到多个事件源,这就是Action接口.一个动作是一个封装下列内容的对象: × 命令的说明(一个文本字符串和一个可选图标): × 执行命令所需 ...
- MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式。
原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Re ...
- P2258 子矩阵(dp)
P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4 ...
- JS中的this是什么,this的四种用法
在Javascript中,this这个关键字可以说是用的非常多,说他简单呢也很简单,说他难呢也很难,有的人开发两三年了,自己好像也说不清this到底是什么.下面我们来看看: 1.在一般函数方法中使用 ...
- idea ssm项目移包报错问题
写完代码之后发现包结构太乱了 想要规划一下 结果报错 这里面的包路径都可以点进去,还是报找不到com.lf.company.entity.Business 后来发现是 在移动前和移动后都存在这个m ...