题目传送门

  高速路出口I

  高速路出口II

题目大意

  给定若干种短串,和文本串$S$,问有多少种方式可以将短串拼成长串。

  显然,你需要一个动态规划。

  用$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 - 动态规划的更多相关文章

  1. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  2. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  3. UVa 103 Stacking Boxes --- DAG上的动态规划

    UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...

  4. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  5. LA 3942 Remember the Word(前缀树&树上DP)

    3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...

  6. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  7. 3942 - Remember the Word

    3942 - Remember the Word 思路:字典树+dp dp[i]前i个字符,能由给的字串组成的方案数,那么dp[i] = sum(dp[i-k]);那么只要只要在字典树中查看是否有字串 ...

  8. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

  9. UVA 3942 Remember the Word (Trie+DP)题解

    思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...

随机推荐

  1. dp入门 石子相邻合并 详细带图讲解

    题目: 有N堆石子,现要将石子有序的合并成一堆,规定如下: 1.每次只能移动相邻的2堆石子合并  2.合并花费为新合成的一堆石子的数量. 求将这N堆石子合并成一堆的总花费最小(或最大). 样例: 输入 ...

  2. Python datetime获取详细时间

    说明:datetime是date和time的结合体,包含了date和time的所有信息 datetime常见用法: 1.datetime.datetime.now()返回一个UTC时间的datetim ...

  3. Subway (树中心 + 树hash)

    首先找到树的中心或者中心,我这里是找中心,因为我们需要找一个相同的起点,然后最多2个中心就是树的宽度为偶数时,奇数时为1个. 找到之后需要对树进行hash,使得每个点都具备独特性,使之树的形态能够保证 ...

  4. Linux 进程间通讯

    一.Linux 下进程间通讯方式 1)管道(Pipe)及有名管道(named pipe): 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...

  5. 入坑tensorflow

    win10 CPU版,anaconda prompt命令行一句话,pip install --upgrade tensorflow搞定.比caffe好装一万倍. gpu版没装成,首先这个笔记本没装cu ...

  6. linux文件系统的用户和权限管理

    1. 为什么要有用户的概念? 多用户,多任务业务对系统资源的隔离产生需求 2. linux 用户的分类? 2.1. 管理员 拥有操作所有文件的权限 2.2. 普通用户 2.2.1. 普通登录用户 2. ...

  7. Python 构造一个可接受任意数量参数的函数

    为了能让一个函数接受任意数量的位置参数,可以使用一个* 参数 在这个例子中,rest 是由所有其他位置参数组成的元组.然后我们在代码中把它当成了一个序列来进行后续的计算

  8. numpy元素级数组函数

    一元函数 abs, fabs 计算整数.浮点数或复数的绝对值.对于非复数值,可以使用更快的fabs. sqrt 计算各元素的平方根.相当于arr ** 0.5 sqare 计算各元素的平方.相当于ar ...

  9. eos中BM与有BM特色的去中心化。区块链世界,白皮书为共识,代码为法律。

    比特币挖矿是谁算力高,谁更容易挖到新的比特币,而BM认为这太浪费资源了,于是设计了DPoS:在DPoS系统里,大家不再挖矿.而是指定几个人负责记账,不叫矿工,而叫见证人.比特股里开始是101人,EOS ...

  10. JXNU暑期选拔赛

    最小的数 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submissi ...