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. Windows 9立即公布了

    Windows 9技术预览版可能于今晚在美国旧金山举办的Windows公布会上宣布.下一代的Windows名称最有可能是Windows或Windows 9.网友猜想也可能是WIndows X或Wind ...

  2. oc const 关键字 对指针的理解

    /* int const *p; *p是常量, p是变量 const int *p; *p是常量, p是变量 int * const p; *p是变量, p是常量 const int * const ...

  3. 使用引导扇区维护工具BOOTICE编辑系统启动列表BCD文件

    使用引导扇区维护工具BOOTICE编辑系统启动列表BCD文件 系列文章: 笔记本电脑提速之加装内存条.SSD固态硬盘.光驱位换SSD固态硬盘 笔记本ThinkPad E430c加装内存和SSD固态硬盘 ...

  4. 怎样设置mysql远程訪问

     Mysql默认是不能够通过远程机器訪问的,通过以下的配置能够开启远程訪问 在MySQL Server端: 运行mysql 命令进入mysql 命令模式, mysql> use mysql; ...

  5. HBase无法连接ZooKeeper问题

    上次搭建HBase环境后,运行登陆server时,报以下的错误: hadoop@gpmaster logs]$ hbase shell SLF4J: Class path contains multi ...

  6. 一段shell脚本

    //根据入参增加nginx反向代理#!/bin/bash#set -x log_path="./proc/logs/shellExecute.log"log_path_back=& ...

  7. ios13--购物车优化

    // // ViewController.m // 03-综合练习 // #import "ViewController.h" @interface ViewController ...

  8. java普通代码块、静态代码块、默认构造方法的执行顺序

    package test; class Parent{ { System.out.println("父类普通代码块"); } static{ System.out.println( ...

  9. sessionStorage 的使用

    sessionStorage 的使用: sessionStorage.removeItem("data"); sessionStorage.getItem("data&q ...

  10. ResGen.exe”已退出,代码为2 问题处理

    转载自  http://blog.sina.com.cn/s/blog_5f82a1060101d8tm.html 在64位的Windows 7下,用VS2010编译4.0以前的.Net项目会有问题. ...