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 标准输入输出 题目类型:传 ...
随机推荐
- HCIA SWITCHING&ROUTTING 笔记——第一章 TCP/IP基础知识(2)
视频地址:https://ilearningx.huawei.com/courses/course-v1:HuaweiX+EBGTC00000336+Self-paced/courseware/abb ...
- hadoop 空间配置
hadoop-------------- 分布式计算框架. common // hdfs //存储 mapreduce //MR,编程模型. yarn //资源调度. 集群部署----------- ...
- python 流程判断
import getpass# print("hello word") ## name= 'pangxiao'# mix_name=name# print(mix_name,nam ...
- python之基础总结(飞机大战)
一.学习python有一段时间了,总体上手还是挺好的,但是有些东西还是和Java存在着一定的区别,这里主要是通过学习,然后自己去编写一个案例.从中学习到的一些东西,这里分享出来,如果存在不正确的地方还 ...
- .net SHA-256 SHA-1
Framework 4.5 uses SHA-256 algorithm for the signature, and 4.0 uses SHA-1.
- 拦截器中获取不到controller注解问题
刚刚在测试接口的时候发现一个奇怪的问题:通过拦截器获取 controller 类注解,有些能获取到,有些又不能获取到,见鬼了. [环境]: 1. springboot :2.2.0.RELEASE [ ...
- php批量检测并去除BOM头的代码
开发中会遇到BOM头, 导致程序无法执行. 浏览器返回接口如下图: 去除BOM头解决方法:<?phpini_set('memory_limit','1024M'); function check ...
- JAVA操作ORACLE大对象
一:操作CLOB (1)数据库表结构如下: create table CLOB_TEST ( ID VARCHAR2(5) not null, ...
- js 单线程 异步
线程与进程: 进程是系统资源分配和调度的单元.一个运行着的程序就对应一个进程.在windows中,每一个打开的运行的应用程序或后台程序,比如运行中的qq,谷歌浏览器,网易云音乐,资源管理器等都是一个进 ...
- Android NDK 学习之Application.mk
Application.mk file syntax specification Introduction: This document describes the syntax of Applica ...