【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word
题目:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description

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 , 1S
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 思路:
将一个字符串按照所给单词分解,求解有多少种分解方案。DP + Trie加速。定义d[i]为后缀i的分解方案数,可以得到状态转移方程:
d[i] += d[i+len[x]]
其中x为后缀i前缀的标号,len为标号为x的单词长度。
但如何快速确定后缀i的前缀呢?这里用到了Trie,在find(后缀i)的路径上有val标记的都为后缀i的前缀,只需要O(n)的时间可以构造出p。 代码:
#include<cstdio>
#include<cstring>
#include<vector>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxnode = * + ;
const int sigma_size = ; // 字母表为全体小写字母的Trie
struct Trie {
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void clear() { sz = ; memset(ch[], , sizeof(ch[])); }
int idx(char c) { return c - 'a'; } void insert(const 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;
} // 找字符串s的长度不超过len的前缀
void find_prefixes(const char *s, int len, vector<int>& ans) { //构造ans
int u = ;
for(int i = ; i < len; i++) {
if(s[i] == '\0') break;
int c = idx(s[i]);
if(!ch[u][c]) break; //不存在该字符 查找失败退出
u = ch[u][c]; //否则往下找
if(val[u] != ) ans.push_back(val[u]); // 找到一个前缀
}
}
//find_prefixes操作以vector形式返回满足要求的字串标号val
//find操作需要对查找操作进行长度限制 以节省时间
}; const int maxl= + ;
const int maxw= +; //单词个数
const int maxwl= + ;
const int MOD = ; char text[maxl];
int d[maxl],len[maxw];
Trie trie; int main(){
int kase=,n;
while(scanf("%s%d",text,&n)==){
trie.clear(); //clear()
char word[maxwl];
FOR(i,,n+){ //标号为1..n
scanf("%s",word);
len[i]=strlen(word); //记录word长度 节约了一个char[][]
trie.insert(word,i); //构造Trie val为word标号
} // DP
memset(d,,sizeof(d));
int L=strlen(text);
d[L]=; // ? :DP边界 只要后缀中有前缀单词 则 += 1
for(int i=L-;i>=;i--){
vector<int> p; //存储
trie.find_prefixes(text+i,L-i,p);
//寻找在后缀text+i中以前缀形势出现过的所有单词 //text+i为字串text[i..L)
FOR(j,,p.size())
d[i] = (d[i]+d[i+len[p[j]]]) % MOD; //i+len-1为单词尾
}
printf("Case %d: %d\n",++kase,d[]);
}
return ;
}
【暑假】[实用数据结构]UVAlive 3942 Remember the Word的更多相关文章
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
UVAlive 4670 Dominating Patterns 题目: Dominating Patterns Time Limit: 3000MS Memory Limit: Unkn ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- 【暑假】[实用数据结构]UVAlive 3026 Period
UVAlive 3026 Period 题目: Period Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
UVAlive X-Plosives 思路: “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物” 如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到K ...
随机推荐
- EL表达式中如何截取字符串
EL表达式中如何截取字符串 可以截取,用fn函数:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/ ...
- js实现网页图片延时加载的原理和代码 提高网站打开速度
有时我们看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对 ...
- CQRS学习——Dpfb以及其他[引]
[Dpfb的起名源自:Ddd Project For Beginer,这个Beginer自然就是博主我自己了.请大家在知晓这是一个入门项目的事实上,怀着对入门者表示理解的心情阅读本系列.不胜感激.] ...
- 【高斯消元】BZOJ 1770: [Usaco2009 Nov]lights 燈
Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...
- Recommender Systems协同过滤
第一部分是学习ID3时候积累的. 一.以前写的基础知识 1.信息:是用来消除不确定性的度量,信息量的大小,由所消除的不确定性的大小来计量(香农). 2.由于不确定性是由随机性引起的,所以用概率来描述和 ...
- PAT-乙级-1010. 一元多项式求导 (25)
1010. 一元多项式求导 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 设计函数求一元多项式的导数.(注:xn(n为整数)的一 ...
- MCM试题原文及翻译 AB题 2014美国数学建模竞赛
MCM试题原文及翻译 AB题 2014美国数学建模竞赛 原创翻译,如有瑕疵,敬请谅解. 转载请注明:过客小站 » MCM试题原文及翻译 AB题 2014美国数学建模竞赛 PROBLEM A: The ...
- linux ubuntu卸载软件
1.通过deb包安装的情况: 安装.deb包: 代码:sudo dpkg -i package_file.deb反安装.deb包: 代码:sudo dpkg -r package_name 2.通过a ...
- FileZilla Server 防火墙端口开启设置 windows 2008 win
入站规则 添加21端口, 程序FileZilla server.exe 出站规则 %SystemRoot%\System32\ftp.exe
- Git教程之删除文件(8)
在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交: