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的更多相关文章

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

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

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

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

  3. uva 1401 dp+Trie

    http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVA 1401 Remember the Word

    字典树优化DP                                Remember the Word Time Limit: 3000MS   Memory Limit: Unknown ...

  5. 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) ...

  6. UVA 1401 Remember the Word(用Trie加速动态规划)

    Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...

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

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

  8. UVA - 1401 Remember the Word(trie+dp)

    1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...

  9. 大白书 209 remember the word

    F - Remember the Word Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Sub ...

随机推荐

  1. scikit-learn: isotonic regression(保序回归,非常有意思,仅做知识点了解,但差点儿没用到过)

    http://scikit-learn.org/stable/auto_examples/plot_isotonic_regression.html#example-plot-isotonic-reg ...

  2. USRP通信的结构体和常量(上位机、下位机共用)

    fw_common.h包括了USRP固件和上位机共用的代码,寄存器地址映射.结构体定义等 #include <stdint.h> /*! * Structs and constants f ...

  3. framework/base子目录

    framework/base下各子目录 ~/src/aosp_master/frameworks $ tree base/ -L 1 base/ ├── Android.bp ├── Android. ...

  4. tiny4412 裸机程序 三、关闭看门狗和调用C程序【转】

    本文转载自:http://blog.csdn.net/eshing/article/details/37112779 一.原理说明 上是章中大家可能有会觉得奇怪,CPU不是有看门狗嘛?为什么CPU没有 ...

  5. atcoder 076

    日本人的比赛 C:如果两个数差了大于1无解,否则分类讨论 #include<bits/stdc++.h> using namespace std; typedef long long ll ...

  6. Linux 文件和目录操作 - cd - 切换目录

    命令详解 重要星级: ★★★★★ 功能说明: cd 命令是 "change directory" 中每个单词的首字母缩写,其功能是从当前工作目录切换到指定工作目录. 语法格式: c ...

  7. P3399 丝绸之路(线性二维dp)

    P3399 丝绸之路 题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚. ...

  8. web自动化测试—selenium游览器下拉框操作

    # coding=utf-8'''下拉框实战思路导包:from selenium.webdriver.support.select import Select #下拉框select from sele ...

  9. javascript中for...in和for...of的区别

    for...of循环是ES6引入的新的语法. for...in遍历拿到的x是键(下标).而for...of遍历拿到的x是值,但在对象中会提示不是一个迭代器报错.例子如下: let x; let a = ...

  10. $P2299 Mzc和体委的争夺战$

    \(problem\) #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef long lon ...