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]代表前 ...
随机推荐
- 分享一个js加密的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python +uiautomator 安卓UI控件操作
一.搭建环境 准备:win7.JDK.androidSDK(adt-bundle-windows-x86_64-20140702\sdk).Appium.安卓模拟器(真机也可以),可以到这个地址下载h ...
- 使用C库制作DLL
一.用C编写制作 DLL 如下图所示,是在C++的基础上新建的项目工程: 新建项目的工程文件中有.cpp文件. 由于我们是用C库制作的DLL,显然用C++来编写的是不合适的,我为什么用C库,而不用C+ ...
- Java生产者与消费者(上)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 生产与消费者模式,是编程中最常用的模式之一,在多线程中应用比较明显.个人理解:在自助餐厅,厨师在不断 ...
- POJ 2828 线段树单点更新 离线搞
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- java根据url获取完整域名
private String getDomain(String destination){ if(destination==null||destination.trim().equals(" ...
- Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045-RFC2049,上面有MIME的详细规范.Base64编码可用于在HTTP环境下传递较长的标识信息.例如 ...
- Codeforces Round #195 (Div. 2) 少部分题解
太困了于是没做...第二天看题蘑菇题居多就只切了简单的两个... A:直接输出... int main() { //FIN; //FOUT; int x,y; cin>>x>> ...
- BZOJ3530: [Sdoi2014]数数(Trie图,数位Dp)
Description 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3 ...
- 如何优雅的写UI——(1)MFC六大核心机制-程序初始化
很多做软件开发的人都有一种对事情刨根问底的精神,例如我们一直在用的MFC,很方便,不用学太多原理性的知识就可以做出各种窗口程序,但喜欢钻研的朋友肯定想知道,到底微软帮我们做了些什么,让我们在它的框架下 ...