LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.
Since Jiejie can't remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie's only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.
The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.
Input
The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.
The second line contains an integer S , 1
S
4000 .
Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.
There is a blank line between consecutive test cases.
You should proceed to the end of file.
Output
For each test case, output the number, as described above, from the task description modulo 20071027.
Sample Input
abcd
4
a
b
cd
ab
Sample Output
Case 1: 2
题意:
该题为大白(刘汝佳。入门经典训练指南)上一道例题。p209。
思路:
dp[i]=sum(dp[i+len(x)])
dp[i]表示从字符i开始的字符串即后缀(s[i..L])的分解方案数。
x为是(s[i..L]的前缀。
详细见代码:
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int md=400110;//单词数乘上单词长度。顶多一个单词一条路径
const int ssz=26;
const int maxn=300010;
const int mod=20071027;
char words[maxn];
int dp[maxn]; struct Trie
{
int ch[md][ssz];
int val[md];
int sz;
void init()
{
sz=1;
val[0]=0;
memset(ch[0],0,sizeof ch[0]);
}
int idx(char c)
{
return c-'a';
}
void inser(char *s)
{
int u=0,n=strlen(s);
for(int i=0; i<n; i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof ch[sz]);
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=n;//可以传参数初始化
}
void sear(char *s,int p,int len)
{
int u=0;
for(int i=0; i<len; i++)//字符一个一个查找
{
int c=idx(s[i]);
if(!ch[u][c])//前缀搜完
return ;
u=ch[u][c];
if(val[u])
dp[p]=(dp[p]+dp[p+val[u]])%mod;
}
}
} tree;
int main()
{
int i,s,len,cas=1;
char tmp[110]; while(~scanf("%s",words))
{
scanf("%d",&s);
len=strlen(words);
tree.init();//开始忘了初始化。调了半天。。。。。
dp[len]=1;//注意这个位置的初始化!
for(i=0; i<s; i++)
{
scanf("%s",tmp);
tree.inser(tmp);
}
for(i=len-1; i>=0; i--)
{
dp[i]=0;
tree.sear(words+i,i,len-i);
}
printf("Case %d: %d\n",cas++,dp[0]);
}
return 0;
}
LA 3942 Remember the Word(前缀树&树上DP)的更多相关文章
- LA 3942 - Remember the Word 字典树+DP
看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
- 洛谷P4426 毒瘤 [HNOI/AHOI2018] 虚树+树上dp
正解:虚树+树上dp 解题报告: 传送门! 首先解释一下题意趴,,,语文70pts选手已经开始看不懂题辣QAQ 大概就是个给一个图,求独立集方案,且保证图是联通的,边的数量最多只比点多10 首先思考如 ...
- luoguP4383 [八省联考2018]林克卡特树(树上dp,wqs二分)
luoguP4383 [八省联考2018]林克卡特树(树上dp,wqs二分) Luogu 题解时间 $ k $ 条边权为 $ 0 $ 的边. 是的,边权为零. 转化成选正好 $ k+1 $ 条链. $ ...
- [LA 3942] Remember the Word
Link: LA 3942 传送门 Solution: 感觉自己字符串不太行啊,要加练一些蓝书上的水题了…… $Trie$+$dp$ 转移方程:$dp[i]=sum\{ dp[i+len(x)+1]\ ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- 【题解】彩色树 51nod 1868 虚树 树上dp
Prelude 题目在这里:ο(=•ω<=)ρ⌒☆ Solution 蒟蒻__stdcall的第一道虚树题qaq. 首先很容易发现,这个排列是假的. 我们只需要求出每对点之间的颜色数量,然后求个 ...
- 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+ ...
随机推荐
- EAN 通用商品条形码
商品条形码是指由一组规则排列的条.空及其对应字符组成的标识,用以表示一定的商品信息的符号.其中条为深色.空为纳色,用于条形码识读设备的扫描识读.其对应字符由一组阿拉伯数字组成,供人们直接识读或通过键盘 ...
- 「操作系统」: Conditional Move Instructions(trap)
Not all conditional expressions can be compiled using conditional moves. Most significantly, the abs ...
- cocos2d-x游戏开发系列教程-超级玛丽05-CMMenuScene
代码下载链接 http://download.csdn.net/detail/yincheng01/6864893 解压密码:c.itcast.cn 背景 上一篇博文提到appDelegate,在该类 ...
- Windows Azure 社区新闻综述(#75 版)
欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure 的社区推动新闻.内容和对话.以下是本周的亮点. 文章.视频和博客文章 · PowerShell 对 Windows A ...
- 配置php网页显示错误
发现问题比解决问题重要,使用php集成开发环境Appserv网页会提示各种语法错误,但自己配置开发环境无法提示错误,特别当不是语法错误,比如com组件的调用,当不知道问题出现在何处,是不能忍受的,这时 ...
- 警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的陷阱
mer_stage 表有 216423 条记录,DDL: CREATE TABLE `mer_stage` ( `STAGE_ID` int(11) NOT NULL AUTO_INCREMENT, ...
- C++中,如何定义和使用指向成员函数的指针
/*** 定义指向成员函数的指针变量的形式 : 成员函数返回类型 (类名∷*指针变量名)(参数列表)* 成员函数指针变量值的形式 : &类名∷成员函数名;* 成员函数指针变量使用形式 : (对 ...
- JS给元素增加className
function(element,value) //给元素添加className { if(!element.className) { element.className=value; } else{ ...
- shell脚本中每次读取文件的一行
写法一: #!/bin/bash while read linedo echo $line #这里可根据实际用途变化 done < file #需要读取的文件 ...
- ArrayList集合-[习题]--C#
:向集合中添加10个元素,计算平均值,求最大.最小值. ; list.AddRange(, , , , , , , , }); int Max, Min; Max = Min = (]; ; i &l ...