LA、Remember the Word (字典树, 简单dp)
题意: 给你一个初始串 S,strlen(s) <= 3e5 然后给你 n 个单词。 n <= 4000, 每个单词的长度不超过 100 ;
问你这个初始串,分割成若干个单词的连接的方案;(这些单词必须是给定的n个单词中的任意一个,一个单词可以被使用多次。)
解: 将 n 个单词建个字典树;
dp[ i ] 表示,S的 0 ~ i - 1 切割成若干个 单词的方案数;
枚举S, 枚举到 当前位置 i; 然后就在字典树找,以 S 的 i + 1 开始的, 能不能找到一个单词与之匹配;
若能找到, 假设单词为 i + 1 ~ j; 那么就有 dp[ j + 1 ] = ( dp[ j + 1 ] + dp[ i ] ) % mod
这只是个简单dp; 和字典树的简单应用的结合。
#include <bits/stdc++.h>
#define LL long long
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define mem(i, j) memset(i, j, sizeof(i))
#define pb push_back
using namespace std; const int mod = ; const int N = 3e5 + , M = 4e5 + ;
char s[N], b[];
int a[M][], tot, vis[N], dp[N];
int get(char x) {
return x - 'a';
}
void join() {
int now = ;
rep(i, , (int)strlen(b) - ) {
int id = get(b[i]);
if(!a[now][id]) {
mem(a[tot], ); vis[tot] = ;
a[now][id] = tot++;
}
now = a[now][id];
}
vis[now] = ;
}
int main() {
int cas = ;
while(~scanf("%s", s)) {
int n; scanf("%d", &n); tot = ;
mem(a[], );
rep(i, , n) {
scanf("%s", b); join();
}
mem(dp, ); dp[] = ;
rep(i, , (int)strlen(s) - ) {
int now = ;
rep(j, i, (int)strlen(s) - ) {
int id = get(s[j]);
if(!a[now][id]) break;
now = a[now][id];
if(vis[now]) {
dp[j + ] = (dp[j + ] + dp[i]) % mod;
}
}
}
printf("Case %d: ", ++cas);
printf("%d\n", dp[(int)strlen(s)]);
}
return ;
}
LA、Remember the Word (字典树, 简单dp)的更多相关文章
- UVA1401 Remember the Word 字典树维护dp
题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- hdu3016 线段树+简单DP
以每个方块左右坐标区间为节点建立字典树,每个节点保存这个区间对应的方块的下标,将方块按照高度排序. 如何得到第i个方块可以移动到的两个方块呢?将所有方块排完序,将前i-1个方块放入字典树,根据第i个方 ...
- 【HDU - 5845】Best Division(xor-trie、01字典树、dp)
BUPT2017 wintertraining(15) #7E 题意 把数组A划分为k个区间,每个区间不超过L长度,每一个区间异或和之和为S.现在求:S不超过X,区间个数的最大值. 且A是这样给你的: ...
- 2018.09.12 poj2376Cleaning Shifts(线段树+简单dp)
传送门 貌似贪心能过啊%%%. 本蒟蒻写的线段树优化dp. 式子很好推啊. f[i]表示覆盖1~i所需的最小代价. 那么显然对于一个区间[li,ri]" role="present ...
- 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...
- LA 3942 - Remember the Word 字典树+DP
看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
- 校内模拟赛 : Rima —— 字典树+树形DP
首先说一下,对一个刚学Trie树的蒟蒻来说(就是我),这道题是一道好题.Trie树比较简单,所以就不详细写了. Rima 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传 ...
随机推荐
- WUSTOJ 1333: Sequential game(Java)
题目链接:1333: Sequential game Description Sequential detector is a very important device in Hardware ex ...
- session和cookie有什么区别?
1.存储位置不同 cookie的数据信息存放在客户端浏览器上. session的数据信息存放在服务器上. 2.存储容量不同 单个cookie保存的数据<=4KB,一个站点最多保存20个Cooki ...
- Windows服务器修改远程桌面默认端口
一.打开注册表(通过开始菜单处输入命令输入 regedit回车即可打开注册表信息,或者Win键+R键打开输入框后输入regedit后回车) 二.打开注册表后,在左侧属性菜单进入下列路径“HKEY_LO ...
- VS 2015 .net UI界面报错总结
一.提示錯誤 解決方法: 右击解决方案点击properties Window Ctrl+W ,P 将Mnaged Pipeline Mode 从Integrated更改为Classic 二.提示錯誤 ...
- 怎样修改element-ui中的样式?
方法一 方法二 使用 /deep/ .homePage /deep/ .el-main { padding: 0; } .homePage为我们要修改组件类名的父级组件样式类名..即使定义一个空的 ...
- Django Rest framework的限流实现流程
目录 一 什么是throttle 二 Django REST framework是如何实现throttle的 三 Django REST framework中throttle源码流程 一 什么是thr ...
- 关于文本设置overflow:hidden后引起的垂直对齐问题
目前有这样的需求,一行标题中,前面为图标,后面是文字,文字要实现一行省略的效果 首先把文字设为:display: inline-block; 然后设置省略: overflow: hidden; wor ...
- EntityFramework进阶(四)- 实现批量新增
本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处 我们可以结合Ado.Net的SqlBulkCopy实现SqlServer数据库的批量新增,其他类型的数据库的批量 ...
- CentOS7 安装记录
起因是想自建一个本地笔记云存储,按照网上的教程搭建,卡在了其中的一个步骤上(文章见https://www.laobuluo.com/1542.html),卡在了如下图的位置,google了一番解决的办 ...
- 【恐怖的数组模拟】Secret Poems - HihoCoder - 1632
Secret Poems - HihoCoder - 1632 图一 图二 Following the order indicated by arrows, you can get “THISISAV ...