LA 3942 - Remember the Word 字典树+DP
给出一个由S个不同单词组成的字典和一个长字符串,把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法?比如有4个单词a 、b 、cd、ab,则abcd有两种分解方法:a+b+cd和ab+cd
用DP来做的话,设dp[i]表示从i开始的字符串到结束的分解方案数,则d[i]=sum{d[ i + len(x)] 单词x是 S[i ……len-1]的前缀。。
故从右向左看,枚举前缀,如果有这个单词则加上d[i]。
PS:用数组实现的话会更快点,我觉得指针好写。。。还有就是这题不释放空间也可以过。(也更快点)。
#include<cstdio>
#include<cstring>
const int MAXN=300000+10;
const int MLEN=26;
const int mod=20071027;
char s[MAXN],word[MAXN];
int dp[MAXN]; struct node
{
node* next[MLEN];
bool isEnd;
node(){ memset(next,0,sizeof(next)); isEnd=false;}
}; struct Trie
{
node *root; inline int index(char &c){return c-'a';} Trie() {root=new node;}
void init() {root=new node;}
void insert(char *str)
{
node *p=root;
int len=strlen(str);
for(int i=0;i<len;i++)
{
int id=index(str[i]);
if(p->next[id]==NULL)
{
node *t=new node;
p->next[id]=t;
}
p=p->next[id]; if(i==len-1)
{
p->isEnd=true;
return;
}
}
} void query(char *str,int start)
{
int len=strlen(str);
node *p=root;
int res=0;
for(int i=start;i<len;i++)
{
int id=index(str[i]);
if(p->next[id]==NULL)
break; p=p->next[id];
if(p->isEnd)
{
res+=dp[i+1];
res%=mod;
}
}
dp[start]=res;
} void del(node *root)
{
if(root==NULL)
return; for(int i=0;i<26;i++)
if(root->next[i]!=0)
del(root->next[i]); delete root;
}
}trie; int main()
{
int kase=1;
while(scanf("%s",s)!=EOF)
{
//初始化trie
memset(dp,0,sizeof(dp));
trie.init(); int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",word);
trie.insert(word);
}
int len=strlen(s)-1;
dp[len+1]=1;
for(int i=len;i>=0;i--)
{
trie.query(s,i);
// printf("%d\n",dp[i]);
}
printf("Case %d: %d\n",kase++,dp[0]);
trie.del(trie.root);
}
}
LA 3942 - Remember the Word 字典树+DP的更多相关文章
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
- Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp
C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- Trie + DP LA 3942 Remember the Word
题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...
- UVA1401 Remember the Word 字典树维护dp
题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...
- UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...
- CF456D A Lot of Games (字典树+DP)
D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...
- HDU5715 XOR 游戏 二分+字典树+dp
当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...
随机推荐
- 分享一个css3学习使用的选择器手册
http://www.haorooms.com/tools/css_selecter/
- NOI2017整数
NOI2017 整数 题意: 让你实现两个操作: 1 \(a\) \(b\):将\(x\)加上整数\(a \cdot 2 ^ b\),其中 \(a\)为一个整数,\(b\)为一个非负整数 2 \( ...
- 今日SGU 5.5
SGU 114 题意:求一个点到其他点的距离总和最小,距离的定义是x轴距离乘以那个点的人数p 收获:带权中位数,按坐标排序,然后扫一遍,最后权值超过或等于总权值的一半时的那个点就是答案,证明暂无 #i ...
- C++遍历目录+_finddata_t结构体用法
Struct _finddata_t是用来存储文件各种信息的结构体,使用这个结构体要引用的头文件为“ #include <io.h>”它的结构体定义如下: struct _finddata ...
- Oracle数据库安装时 environment variable path 大于 1023
提示的内容如下: 打开系统的环境变量设置, 编辑Path,全选将其中的路径全部复制出来放到文本文档中.新建一个系统变量取名Path_Old_1,剪切Path中的所有变量复制进path1然后保存,将Pa ...
- Dos图像复制成序列
rem 输入1.png,在当前文件下复制.0000.png--0002.png rem 注:way2是不等待0001.png运行完就開始运行下一个了. rem 假设要等待上一个运行完后,再往下顺弃运行 ...
- 火狐—火狐浏览器中的“HttpWatch”
在IE下通过HttpWatch能够查看HTTP请求的相关细节.这对我们分析程序的运行效率很有帮助,但是在火狐浏览器中的难道就没有相似的工具了吗?答案是否定的--火狐浏览器中也有.在火狐浏览器中该工具叫 ...
- CListCtrl 隔行变色
响应消息 ON_NOTIFY(NM_CUSTOMDRAW, ListCtrl的ID, OnNMCustomdrawList) 实现函数OnNMCustomdrawList void CFinishWe ...
- 亚马逊AWS学习——EC2的自己定义VPC配置
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/47153421 本文出自[我是干勾鱼的博客] 1 网络配置 EC2即亚马逊AWS云 ...
- magento getCarriers 分析
完整的设置订单追踪信息的时候我们可能会用到它.在后台中他在这里设置: 有的时候我们想要设置自己定义的 carrier 比如 顺丰 申通 圆通 ..等等 我们能够先从 magento api 入手分析 ...