看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1943

给出一个由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的更多相关文章

  1. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  2. LA 3942 Remember the Word(前缀树&树上DP)

    3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...

  3. Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp

    C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...

  4. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. Trie + DP LA 3942 Remember the Word

    题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...

  6. UVA1401 Remember the Word 字典树维护dp

    题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...

  7. UVALive 3942 字典树+dp

    其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...

  8. 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 ...

  9. HDU5715 XOR 游戏 二分+字典树+dp

    当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前 ...

随机推荐

  1. 趣题: 按二进制中1的个数枚举1~2^n (位运算技巧)

    ; ; k <= n; k++){ << k)-,u = << n; s < u;){ ;i < n;i++) printf(-i)&); print ...

  2. 洛谷 P1801 黑匣子_NOI导刊2010提高(06)(未完)

    P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...

  3. BZOJ1444: [Jsoi2009]有趣的游戏(Trie图,矩乘)

    Description Input 注意 是0<=P, n , l, m≤ 10. Output Sample Input input 1 3 2 2 1 2 1 2 AB BA AA inpu ...

  4. STM32之CAN通讯接收过滤器过滤分析

    一.前言 学习了CAN通讯,底层的东东CAN控制器已经帮你处理完成,也就是CAN通讯协议已经做好,你按协议格式往对应的位扔数据发送就好,所以使用CAN通讯,我们只需要去关心制定发送的数据间的协议,也就 ...

  5. sql server向表里添加字段

    ADD mcTypeE VARCHAR(20) NULL,mcGoodsE VARCHAR(20) NULL, mcTypeF VARCHAR(20) NULL,mcGoodsF VARCHAR(20 ...

  6. Iptables-主机防火墙设置

    基于Iptables构建主机防火墙 Iptables优点: 数据包过滤机制,它会对数据包包头数据进行分析. 1.1.1 加载相关薄块到内核 [root@centos7 ~]# lsmod | egre ...

  7. python之经典猜数字

    题目:猜数字1.让用户输入1-20,猜数字,可以猜5次.2.每次有提示,大了,或者小了!3.如果超过5次,提示game over. # !/usr/bin/env python # -*- codin ...

  8. vc如何让打开的子窗口默认是最大化的

    vc如何让打开的子窗口默认是最大化的 浏览: 3554 | 更新: 2011-04-09 17:04 1 0     加入杂志加入杂志 摘要:关于vc如何让打开的子窗口默认是最大化的深入研究.   步 ...

  9. Loading half a billion rows into MySQL---转载

    Background We have a legacy system in our production environment that keeps track of when a user tak ...

  10. ds1302模块的一个arduino程序

    /* * 读写DS1302 时钟芯片 * @author Yangtf * 很棒的文档 http://www.21ic.com/jichuzhishi/datasheet/DS1302/data/18 ...