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]代表前 ...
随机推荐
- 经典的横线中间文字css布局---flex布局
html: <div class="title"> <div class="line"></div> <div cla ...
- 今日SGU 5.9
SGU 297 题意:就是求余数 收获:无 #include<bits/stdc++.h> #define de(x) cout<<#x<<"=" ...
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory(转)
1. Spring整合MyBatis切换SqlSessionFactory有两种方法,第一. 继承SqlSessionDaoSupport,重写获取SqlSessionFactory的方法.第二.继承 ...
- erlang虚拟机代码运行原理
erlang是开源的,非常多人都研究过源码.可是.从erlang代码到c代码.这是个不小的跨度.并且代码也比較复杂. 所以这里,我利用一些时间,整理下erlang代码的运行过程.从erlang代码编译 ...
- iOS Dev (51)加急审核
https://developer.apple.com/appstore/contact/? topic=expedite
- 制作 wordpress 博客静态化到本地
wget 克隆 wordpress 博客镜像 wget -e robots=off -w 1 -xq -np -nH -pk -m -t 1 -P "./wordpress.org" ...
- [51Nod]NOIP2018提高组省一冲奖班模测训练(四)翻车记+题解
链接 下午5点的时候,突然想起来有这个比赛,看看还有一个小时,打算来AK一下,结果因为最近智商越来越低,翻车了,我还是太菜了.上来10分钟先切掉了C和A,结果卡在了B题,唉. A.砍树 一眼题,两遍树 ...
- 《四》JAVA 字符输入输出流
那么这篇博客我们讲的是字节输入输出流:Reader.Writer(下图红色长方形框内),红色椭圆框内是其典型实现(FileReader.FileWriter) ①.为什么要使用字符流? 因为使用字节流 ...
- vue使用marked.js实现markdown转html并提取标题生成目录
html: <template> <div class="wrapper"> <div class="container"> ...
- BZOJ2527: [Poi2011]Meteors(整体二分)
Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby gala ...