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是从最后一 ...
随机推荐
- 从零开始一起学习SLAM | 三维空间刚体的旋转
刚体,顾名思义,是指本身不会在运动过程中产生形变的物体,如相机的运动就是刚体运动,运动过程中同一个向量的长度和夹角都不会发生变化.刚体变换也称为欧式变换. 视觉SLAM中使用的相机就是典型的刚体,相机 ...
- Windows jmeter配置
JMeter是Apache软件基金会的产品,用于对提供静态的和动态的资源服务器性能的测试.是一款很方便的测试软件. JMeter 要依附Java SE 环境 所以在启用JMeter之前要安装JAVA ...
- Lua class
local _class = {} function class(super) local class_type = {} class_type.ctor = false class_type.sup ...
- flask orm
#查询 #查第一行 session.query(User.id,User.userName,User.password).first() #查所有行 sessi ...
- mysql字符集问题,及排序规则
字符集问题: 基本概念 • 字符(Character)是指人类语言中最小的表义符号.例如’A'.’B'等:• 给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符,这一数值就是字符的编码(E ...
- leetCodeReorderList链表合并
原题 Given a singly linked list L: L0?L1?-?Ln-1?Ln, reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?- You must do ...
- python遍历某一位置所有文件夹中的文件
通过多次遍历达到找出所有文件的目的 import os rootdir=["d:/77"] c=[] for i in rootdir: for parent,dirnames,f ...
- python 翻译爬虫
import urllib.request import urllib.parse import json while 1: content=input("请输入要翻译的内容:") ...
- Rpgmakermv(15) PH任务插件
插件介绍 一个用来简单显示任务阶段的任务书 使用方法 插件安装 下载js文件放置到游戏目录/plugins目录下.打开插件管理器,选择PH_QuestBook.js并开启. 插件参数 Show in ...
- Java基础(basis)-----抽象类和接口详解
1.抽象类 1.1 abstract修饰类:抽象类 不可被实例化 抽象类有构造器 (凡是类都有构造器) 抽象方法所在的类,一定是抽象类 抽象类中可以没有抽象方法 1.2 abstract修饰方法:抽象 ...