UVa Live 3942 Remember the Word - Hash - 动态规划
显然,你需要一个动态规划。
用$f[i]$表示拼出串$S$前$i$个字符的方案数。
转移是显然的。枚举上一个拼接的串的长度,然后判断它是否存在,如果存在就把$f[i]$加上$f[i - l]$。
这个判断存在可以用Hash。当然可以对每个短串的反串建立Trie树,然后在Trie树上查一查$i$往前会走到长度为哪些的终止状态。
由于我懒,不想写Trie树,直接用平板电视的hash表,然后慢得起飞。
Code
/**
* UVa Live
* Problem#3942
* Accepted
* Time: 603ms
*/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
using namespace __gnu_pbds;
typedef bool boolean; #define ui unsigned int const int N = 3e5 + , M = , L = ;
const ui base = ; int n;
char S[N], buf[L];
ui ha[N], ps[N];
cc_hash_table<int, boolean> mp[L]; inline void prepare() {
ps[] = ;
for (int i = ; i < N; i++)
ps[i] = ps[i - ] * base;
} inline boolean init() {
if (scanf("%s", S + ) == EOF) return false;
scanf("%d", &n);
for (int i = ; i < L; i++)
mp[i].clear();
int l;
ui s;
for (int i = ; i <= n; i++) {
scanf("%s", buf);
for (l = , s = ; buf[l]; l++)
s = (s * base) + buf[l];
mp[l][s] = true;
}
return true;
} int f[N];
inline void solve() {
f[] = , ha[] = ;
n = strlen(S + );
for (int i = ; i <= n; i++)
ha[i] = ha[i - ] * base + S[i];
for (int i = ; i <= n; i++) {
f[i] = ;
for (int j = ; j < L && j <= i; j++) {
ui s = ha[i] - ha[i - j] * ps[j];
if (mp[j].find(s) != mp[j].end())
f[i] = (f[i] + f[i - j]) % M;
}
}
printf("%d\n", f[n]);
} int kase = ;
int main() {
prepare();
while(init()) {
printf("Case %d: ", ++kase);
solve();
}
return ;
}
UVa Live 3942 Remember the Word - Hash - 动态规划的更多相关文章
- 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[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- UVa 103 Stacking Boxes --- DAG上的动态规划
UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- 3942 - Remember the Word
3942 - Remember the Word 思路:字典树+dp dp[i]前i个字符,能由给的字串组成的方案数,那么dp[i] = sum(dp[i-k]);那么只要只要在字典树中查看是否有字串 ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
随机推荐
- HTTP请求解析过程 (简单概括)
1.域名解析 用户输入网址,由域名系统DNS解析输入的网址: 2.TCP的3次握手 通过域名解析出的IP地址来向web服务器发起TCP连接请求,如果3次握手通过,则与web服务端建立了可靠的连接: 3 ...
- java微信小程序调用支付接口
简介:微信小程序支付这里的坑还是有的,所以提醒各位在编写的一定要注意!!! 1.首先呢,你需要准备openid,appid,还有申请微信支付后要设置一个32位的密钥,需要先生成一个sign,得到pre ...
- Linux.超级管理员root-密码破解
Linux.超级管理员root-密码破解 环境: 我是在虚拟机中安装的Linux,然后进行破解的.实体机,应该也是可以的,但我没有尝试过.如果你有实体机,试试吧... 又不要钱~ 在系统启动的时候,会 ...
- 011-Server服务器对象属性
Transfer:第一个页面直接调用第二个页面,执行完第二个页面后不再返回第一个页面,立即响应到客户端浏览器.Execute:第一个页面直接调用第二个页面,执行完第二个页面后再返回第一个页面执行,最后 ...
- jquery.ajax请求aspx和ashx的异同 Jquery Ajax调用aspx页面方法
1.jquery.ajax请求aspx 请求aspx的静态方法要注意一下问题: (1)aspx的后台方法必须静态,而且添加webmethod特性 (2)在ajax方法中contentType必须是“a ...
- 如何用vue组件做个机器人?有趣味的代码
<!DOCTYPE html> <html lang="en"> <div> <meta charset="UTF- ...
- 在运行create_list.sh时候报错:AttributeError: 'module' object has no attribute 'LabelMap'
Traceback (most recent call last):File "/opt/xuben-project/caffe/data/VOC0712/../../scripts/cre ...
- c#Lock学习笔记
转自 http://www.cnblogs.com/tianma3798/p/6290158.html 参考官网 https://docs.microsoft.com/zh-cn/dotnet/cs ...
- [1]windows 内核情景分析---说明
本文说明:这一系列文章(笔记)是在看雪里面下载word文档,现转帖出来,希望更多的人能看到并分享,感谢原作者的分享精神. 说明 本文结合<Windows内核情景分析>(毛德操著).< ...
- Echo团队团队展示
班级:软件工程1916|W 作业:团队作业第一次-团队展示 团队名称:Echo 课程目标:展示团队 成员信息 队员学号 队员姓名 个人博客地址 备注 221600418 黄少勇 http://www. ...