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 标准输入输出 题目类型:传 ...
随机推荐
- Spring Boot使用@Value注解获取配置文件中的属性
获取配置文件的内容——
- (转)基于FFPMEG2.0版本的ffplay代码分析
ref:http://zzhhui.blog.sohu.com/304810230.html 背景说明 FFmpeg是一个开源,免费,跨平台的视频和音频流方案,它提供了一套完整的录制.转换以及流化音视 ...
- react组件懒加载
组件懒加载方式-:react新增的lazy const Alert = lazy(() => import('./components/alert')); export default func ...
- jqgrid属性以及事件
转载 1.属性 参数名称 类型 描述 默认值 是否可以被修改 ajaxGridOptions object 对ajax参数进行全局设置,可以覆盖ajax事件:error,complete 和 befo ...
- PAT-1107 Social Clusters (30 分) 并查集模板
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- php 5.6.36 安装mcrypt
相关扩展安装步骤 libmcrypt downloadUrl:https://sourceforge.net/projects/mcrypt/files/MCrypt/ versionCode:2.5 ...
- Windows环境下实现Jenkins自动化部署
详见:https://blog.csdn.net/Try_harder_every_day/article/details/79170065 Jenkins自动化部署: 几条具体的思路:1.开发人员将 ...
- 用python代码编写象棋界面,棋盘覆盖问题
编写象棋界面 import turtle t=turtle.Pen() t.speed(100) def angle(x,y): t.penup() t.goto(x+3,y+3) t.pendown ...
- Redis未授权访问漏洞复现及修复方案
首先,第一个复现Redis未授权访问这个漏洞是有原因的,在 2019-07-24 的某一天,我同学的服务器突然特别卡,卡到连不上的那种,通过 top,free,netstat 等命令查看后发现,CPU ...
- 透过CountDownLatch窥探AQS
本文来自公众号“Kahuna”,可搜索Alitaba119,欢迎关注,转载请注明出处,非常感谢 “ A synchronization aid that allows one or more thre ...