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是从最后一 ...
随机推荐
- vim自动安装插件Vundle
https://github.com/VundleVim/Vundle.vim Set up Vundle: git clone https://github.com/VundleVim/Vundle ...
- 解决React Native使用Fetch API请求网络报Network request failed
问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...
- Python记录7:函数3,函数对象
#函数对象指的是:函数的内存地址可以像变量值一样去使用,如何使用?def func(): print('from func')#1. 可以被引用# f=func# print(f)# f() #2. ...
- 加快cin读取速度
cin在读取大量数据时会比C里的scanf慢很多,但这并不是cin"无能",而是C++为了兼容C,对cin做了scanf的同步,只要关闭这个同步,cin就会有不弱于scanf的速度 ...
- 设置一个div网页滚动时,使其固定在头部,当页面滚动到距离头部300px时,隐藏该div,另一个div在底部,此时显示;当页面滚动到起始位置时,头部div出现,底部div隐藏
设置一个div网页滚动时,使其固定在头部,当页面滚动到距离头部300px时,隐藏该div,另一个div在底部,此时显示: 当页面滚动到起始位置时,头部div出现,底部div隐藏 前端代码: <! ...
- 20155228 2016-2017-2 《Java程序设计》第1周学习总结
20155228 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 这部分内容是以教材为基础,根据个人的理解来描述的,有的地方的理解和表述可能不规范甚至不正确, ...
- 【2017-03-13】Tsql 数学函数、字符串函数、转换函数、时间日期函数
一.数学函数(针对值类型操作) 1.ceiling():取上限 只要小数点后有数字大于0,整数位自动进1 2.floor():取下限 将小数点位舍去,不管小数点位大小 3.round(四舍五入的值,保 ...
- Spark学习之路 (六)Spark Transformation和Action
Transformation算子 基本的初始化 java static SparkConf conf = null; static JavaSparkContext sc = null; static ...
- linux常用命令:rm 命令
昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所 ...
- yum配置文件中baseurl和mirrorlist的区别
找到yum.repo.d文件夹下的文件,随便打开一个,找到mirrorlist的url,比如: http://mirrorlist.centos.org/?release=6&arch=$ba ...