Remember the Word UVALive - 3942 DP_字典树
每个小单词的长度都是小于等于100的,这是个重要的突破口.
Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 300006
#define mod 20071027
using namespace std;
char str[maxn],wd[maxn];
int tag[maxn],n,f[maxn],m,cas;
struct TRIE{
#define SIGMA 30
int root,ch[maxn][SIGMA],cnt;
void init() {
root = cnt = 0;
memset(ch,0,sizeof(ch));
memset(tag,0,sizeof(tag));
memset(f,0,sizeof(f));
}
void ins(char s[]) {
int len = strlen(s),p = root;
for(int i = 0;i < len; ++i) {
if(!ch[p][s[i]-'a']) ch[p][s[i]-'a'] = ++cnt;
p = ch[p][s[i]-'a'];
}
tag[p] = 1;
}
void solve(int st){
int p = root;
for(int i = st;i <= m; ++i) {
p = ch[p][str[i]-'a'];
if(!p) break;
if(tag[p]) f[st] += f[i + 1],f[st] %= mod;
if(tag[p] && i == m) f[st] += 1;
}
}
}trie;
int main(){
//setIO("input");
while(scanf("%s",str) != EOF) {
scanf("%d",&n),trie.init(),m = strlen(str) - 1;
for(int i = 1;i <= n; ++i) scanf("%s",wd),trie.ins(wd);
for(int i = m;i >= 0; --i) trie.solve(i);
printf("Case %d: %d\n",++cas,f[0]);
}
return 0;
}
Remember the Word UVALive - 3942 DP_字典树的更多相关文章
- UVALive - 3942 (字典树)
递推:$d(i) $表示从第$i$个字符开始到末尾的字符串(即后缀S[i...n])的分解方案数,则$d(i) = \sum {d(i + len(x))} $,其中字符串$x$代表S[i...n]的 ...
- Remember the Word UVALive - 3942(dp+trie)
题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
随机推荐
- DevExpress Report 打印提示one or more margins are set outside the printable area of the page 问题解决
DevExpress Report Print的时候,出现这样的问题:one or more margins are set outside the printable area of the pa ...
- mysql update join优化update in查询效率
数据库版本:5.6.16 update in 修改数据,结果执行时间过慢,一直不出结果. SQL语句及执行计划如下: UPDATE erp_order_extra SET last_time=1231 ...
- 卡片式大学综合英语词汇(Windows Phone 8.1 RT app)
简易卡片式记单词app.词库是原滋原味的大学综合英语词汇,包含语音,使用卡片式设计.离线词库,随时随地记单词. 商店:http://www.windowsphone.com/zh-cn/store/a ...
- ZOJ 3019 Puzzle
解题思路:给出两个数列an,bn,其中an,bn中元素的顺序可以任意改变,求an,bn的LCS 因为数列中的元素可以按任意顺序排列,所以只需要求出an,bn中的元素有多少个是相同的即可. 反思:一开始 ...
- POJ 3624 Charm Bracelet【01背包】
解题思路:直接套公式就能做的01背包, for(i=1;i<=n;i++) { for(v=w[i];v<=m;v++) f[i,v]=max(f[i,v],f[i-1,v-w[i]]+d ...
- Codeforces Round #493 (Div. 1) B. Roman Digits 打表找规律
题意: 我们在研究罗马数字.罗马数字只有4个字符,I,V,X,L分别代表1,5,10,100.一个罗马数字的值为该数字包含的字符代表数字的和,而与字符的顺序无关.例如XXXV=35,IXI=12. 现 ...
- Python 函数部分练习题
函 数 基 础 1.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作2.写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 3.写函数,判断用户传入的 ...
- css——导航栏
导航栏一般用无序列表制作 但出来的导航栏有黑点,还有一些边距 去除黑点我们可以用:list-style-type: none;/*去掉ul前面的点*/ 因为有些标签之间会有默认的边距,所以可以先将边踞 ...
- 前端JS 异常处理实践
前端异常处理,常见的场景是在“异步请求”的操作过程当中,所谓“异常”---就是“不正常”,程序的运行不符合我们的预期. 程序“正常”的处理,是我们在开发过程当中的“重中之重”,是必要的“硬性指标”. ...
- oracle数据库安装教程以及问题和解决方法
一,oracle数据库的下载和安装 1,在oracle官网上下载oracle安装包,运行setup.exe. 2,点击取消“我希望同构My Oracle Support接受安全更新”,以防后患. 3, ...