UVALive 3942 Remember the Word 字典树+dp
/**
题目:UVALive 3942 Remember the Word
链接:https://vjudge.net/problem/UVALive-3942
题意:给定一个字符串(长度最多3e5)和m个单词(每个单词长度最多100)。单词都是不同的。该字符串可以由若干个单词组成,问最多有多少种组合方式。 思路:字典树+dp
用字典树处理好m个单词,定义dp[i]表示从i开始的字符串可以由单词组成的方式数。
那么dp[i] += dp[i+j]; j表示某个单词和字符串的[i,i+j-1]匹配。 */ #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = ;
const int INF = 0x3f3f3f3f;
const int maxnode = 4e5+;///最多可能有多少个节点
const int maxn = 3e5+;
const int sigma_size = ;///26个字母
int dp[maxn];
char s[maxn], ss[maxn];安全1
int ch[maxnode][sigma_size];///由于很大,所以结构体内部放不下。要放在外面。
struct Trie{
int val[maxnode];
int sz;
int idx(char c){return c-'a';} void insert(char *s,int v)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof ch[sz]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
} void query(int sta,int off)
{
int u = ;
int c = idx(s[sta]);
while(s[sta]!='\0'&&ch[u][c]){
if(val[ch[u][c]]){
dp[sta-off] = (dp[sta-off]+dp[sta+])%mod;
}
u = ch[u][c];
c = idx(s[++sta]);
off++;
}
}
}; int main()
{
int cas = ;
int n;
Trie trie;
while(scanf("%s",s)==)
{
scanf("%d",&n);
trie.sz = ;
memset(ch[], , sizeof ch[]);
for(int i = ; i < n; i++){
scanf("%s",ss);
trie.insert(ss,);
}
int m = strlen(s);
memset(dp, , sizeof dp);
dp[m] = ;
for(int i = m-; i>=; i--){
trie.query(i,);
}
printf("Case %d: %d\n",cas++,dp[]);
}
return ;
}
UVALive 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 ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp
C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...
- UVALive 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)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 3942 字典树+dp
其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...
随机推荐
- Android-经常涉及到的权限
Android中配置权限的方法: 在AndroidMainFest.xml中加上以下代码 Android中一些经常涉及到的权限: 添加WiFi以及访问网络的权限: <uses-permissio ...
- .NET/MVC-ViewBag、ViewData、TempData区别
1.ViewData ViewData的生命周期和View相同, 只对当前View有效. 2.TempData TempData保存在Session中, Controller每次执行请求的时候会从Se ...
- ESP8266学习笔记6:ESP8266规范wifi连接操作
一.前言 我整理了从2015年至今关于ESP8266的学习笔记,梳理出来了开发环境.基础功能.进阶学习三大部分.方便自己和他人.可点此查看,欢迎交流. 之前在笔记4<ESP8266的SmartC ...
- 让你的Photoshop编辑制作ICO格式图标文件(ICOFormat支持图标文件插件)
相信非常多制图的朋友都喜欢用PS,可是你能用Photoshop保存为ICO格式图标文件吗?默认肯定不行.不知道是什么原因,大名鼎鼎的图像编辑软件Adobe Photoshop一直不支持导入导出ico格 ...
- 在Eclipse中点击ctrl+h打开搜索界面,范围指定的项目中搜索包含person的文件
ctrl+h ===>File Search Tab ===>在containing text里输入person,scope ==>selected resources,搜索就可以了
- Netty利用ChannelGroup广播消息
在Netty中提供了ChannelGroup接口,该接口继承Set接口,因此可以通过ChannelGroup可管理服务器端所有的连接的Channel,然后对所有的连接Channel广播消息. Serv ...
- js中,三元运算的简单应用(?:)
js中,三元运算的简单应用: var sinOrMul = ""; sinOrMul =(subType=="single")?("<span ...
- CentOS安装glibc-2.14(转)
到http://ftp.gnu.org/gnu/glibc/下载glibc-2.14.tar.xz tar glibc-2.14.tar.gz cd glibc-2.14 mkdir build cd ...
- java数据库编程:JDBC操作及数据库
掌握JDBC操作步骤, 掌握数据库驱动程序配置 可以使用JDBC进行数据库连接. JDBC本身是一个标准,因此操作步骤是固定的,以后只需要修改很少代码就可以达到不同数据库间连接转换功能. 操作步骤: ...
- 【微信小程序】loading标签使用,可自定义时长
前言:loading和wx.showToast的区别: wx.showToast加载的时间长度是需要手动设置的,默认1500ms,而loading标签则可以配合数据加载进行隐藏. 核心就是在数据量较大 ...