题目传送门

  高速路出口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. cocos2d-x C++ (iOS)集成第三方微信分享

    1.新建项目并下载 ShareSDK 1.Cocos2d-x项目环境搭建,不会的童鞋自行面壁哈: 网页链接. 2.ShareSDK iOS版本的 Cocos2d-x 插件是在ShareSDK iOS版 ...

  2. C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)

    Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...

  3. PHP与JSP简单比较

    比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较php和Java的Web开发.以下就几个主要方面进行的比较: 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上手 ...

  4. CentOS下挂载数据盘

    CentOS下挂载数据盘 1.显示磁盘使用情况:#df 2.显示磁盘:#fdisk -l 3.格式化分区:#mkfs.ext4 /dev/vdb1           //注:将/dev/vdb1格式 ...

  5. 关于django编码问题所导致的502错误

    在使用django开发的时候,访问网站出现了502错误,次数也比较平繁 uwsgi+nginx+django 在命令行下提示如下错误 : File "/opt/apps/python3/li ...

  6. BestCoder Round #55 ($)

    C 构造一个矩阵,然后采用矩阵快速幂 #include <iostream> #include <algorithm> #include <string.h> #i ...

  7. codeoforces 975B Mancala

    题意: 一个游戏,有14个洞,每个洞中开始有若干个球或者没有球. 每一步的操作,是将一个洞中的所有球取出,再逆时针放一个球到它的后一个洞,后两个洞,后三个洞....如果当前放的是最后一个,那么下一个又 ...

  8. 浏览器页面请求js、css大文件处理

    当页面引用一个比较大的js和css文件时,会出现较大下载延迟,占用带宽的问题,如果一个应用里有很多这样的js或CSS文件,那么就需要优化了. 比如ext-all.js有1.4M,页面引用这个文件,正常 ...

  9. Python: 字典dict: 相同点

    问题:怎么样在两个字典中找相同点 answer: eg1: 下面2个字典 a={'x':1,'y':2,'z':3},    b={'w':10,'x':11,'y':2}, 1)找相同点: a.ke ...

  10. 特定条件下批量解压文件改变编码,顺便修改.so.0找不到等一些小问题

    直接结论: 1.linux解压文件乱码: unzip -O GBK *.zip 2.linux改变文件内容编码: 安装enca,下载地址:https://github.com/nijel/enca/i ...