UVALive - 3942:Remember the Word
发现字典里面的单词数目多且长度短,可以用字典树保存
f[i]表示s[i~L]的分割方式,则有f[i]=∑f[i+len(word[j])] 其中word[j]为s[i~L]的前缀
注意字典树又叫前缀树,所以用前缀更方便,否则按顺序dp的话就要把字符倒序了
复杂度O(L*l) L为字符串长度,l为单词长度
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 300005
#define MOD 20071027
using namespace std;
struct Trie{
Trie* nxt[];
int v;
Trie(){
for(int i=;i<;i++){
nxt[i]=NULL;
}
v=;
}
};
Trie *root;
void insert(char s[],int vl){
Trie *p=root;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
if(p->nxt[t]){
p=p->nxt[t];
}
else{
p->nxt[t]=new Trie;
p=p->nxt[t];
}
}
p->v=vl;
}
int f[MAXN];
char s[MAXN];
int n,L;
int find(int st){
int ret=;
Trie *p=root;
for(int i=st;i<L;i++){
int t=s[i]-;
if(p->nxt[t]){
p=p->nxt[t];
if(p->v){
(ret+=f[i+])%=MOD;
}
}
else{
return ret;
}
}
return ret;
}
void del(Trie *p){
if(p){
for(int i=;i<;i++){
if(p->nxt[i]){
del(p->nxt[i]);
}
}
delete p;
}
}
int main()
{
int T=;
while(~scanf("%s",s)){
memset(f,,sizeof(f));
del(root);
L=strlen(s);
root=new Trie;
scanf("%d",&n);
for(int i=;i<=n;i++){
char ch[]={};
scanf("%s",ch);
insert(ch,);
}
f[L]=;
for(int i=L-;i>=;i--){
f[i]=find(i);
}
printf("Case %d: %d\n",++T,f[]);
}
return ;
}
UVALive - 3942:Remember the Word的更多相关文章
- UVALive - 3942 Remember the Word
input 字符串s 1<=len(s)<=300000 n 1<=n<=4000 word1 word2 ... wordn 1<=len(wordi)<=10 ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- 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 ...
- 我的VSTO之路(四):深入介绍Word开发
原文:我的VSTO之路(四):深入介绍Word开发 在上一篇文章中,我介绍了Word的对象模型和一些基本开发技巧.为了更好的介绍Word插件开发,我为本文制作了一个Word书签的增强版,具体功能是让用 ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- (Trie) uvalive 3942 Remember the word
题意:告诉你一个母串和子串,能用多少种不同的方案组合出母串. 思路:字典树(显然)+DP DP: dp[i]+=dp[j+1] i<=j<=m-1,且i到j的字符串能在字典树中找到.也就 ...
随机推荐
- MyGod--Beta版本前期报告
下一阶段需要改进完善的功能 1.完善购买功能,商品购买后,将生成申请订单,卖家将收到提醒.卖家在完成订单后,可以选择完成订单,商品将下架. 2.完善搜索功能,将界面中的搜索功能添加进去(简单考虑只搜索 ...
- scrapy 爬取当当网产品分类
#spider部分import scrapy from Autopjt.items import AutopjtItem from scrapy.http import Request class A ...
- Android 4.4 沉浸式透明状态栏
原文链接:http://www.bkjia.com/Androidjc/913061.html 第一种方法 这里写代码片第一种方法,在代码设置: if(VERSION.SDK_INT >= VE ...
- java实现红包的分配算法
个人推测,微信红包在发出的时候已经分配好金额.比如一个10元的红包发给甲乙丙三个人,其实在红包发出去的时候,已经确定了第一个会领取多少,第二个会领取多少金额. 而不是在领取的时候才计算的.下面贴出实现 ...
- windows安装gcc编译器
由于vc6.0对c语言编译不是很好,有些语句是正确的,但是编译却不能通过 所以决定在windows中安装gcc编译器来使用! http://www.cnblogs.com/cryinstall/arc ...
- nodeJs多进程Cluster
在前端页面中,如果我们想进行多进程,我们会用到WebWorker,而在NodeJs中,我们如果想充分利用服务器核心资源,我们会用到Node中Cluster模块 直接上代码吧: const cluste ...
- ( 转 ) CORS 有一次 OPTIONS 请求的原理
刚接触前端的时候,以为HTTP的Request Method只有GET与POST两种,后来才了解到,原来还有HEAD.PUT.DELETE.OPTIONS-- 目前的工作中,HEAD.PUT.DELE ...
- SpringCloud的Bus(一)消息中间件的概念和用途
一.概念与定义 1.Message Broker Message Broker是一种消息验证.消息转换.消息路由的架构模式,用于如: 消息路由到一个或多个目的地 消息转化为其他的表现方式 执行消息的聚 ...
- HTML中的上下标标签的演示
HTML中的上下标标签的演示 #table_head>td { font-weight: bold } tr { text-align: center } 作用 标签 演示代码 呈现效果 上标 ...
- java循环遍历类属性 get 和set值方法
//遍历sqspb类 成员为String类型 属性为空的全部替换为"/"Field[] fields = sqspb.getClass().getDeclaredFields(); ...