UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401
题意
给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串。求有多少种方案可以把这个字符串分解为字典中的单词。
分析
首先强烈吐槽,vjudge上的UVALive 3942怎么都过不了。。。然而题目一模一样的UVA 1401就稳稳地过了。。。很玄学。
根据题意可以想到一个dp,dp[i]表示从第i个字符开始的字符串的分解方案。那么dp[i]=dp[i]+dp[i+len(x)],其中单词x为匹配的前缀。
如此,从后开始枚举i,每次都走一遍trie,找对应前缀的单词节点,然后计数。初始化dp[len]=1,为了让dp[len-1]能正确转移。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
//#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
//const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5 + ;
const int maxm = 4e5 +;
const int mod = ;
const int sigma_size = ;
char s[maxn];
ll dp[maxn];
struct Trie{
int ch[maxm][sigma_size];
bool val[maxm];
int sz;
void init(){
sz=;
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
}
int idx(char c) { return c-'a'; }
void insert(char* s){
int u=,n=strlen(s);
for(int i=;i<n;i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=false;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=true;
}
void query(char* s){
int len=strlen(s);
memset(dp,,sizeof(dp));
dp[len]=;
for(int i=len-;i>=;i--){
int u=;
for(int j=i;j<len;j++){
int c = idx(s[j]);
if(!ch[u][c]) break;
u=ch[u][c];
if(val[u]) dp[i]=(dp[i]+dp[j+])%mod;
}
}
}
};
Trie trie;
char tmp[];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("input.txt", "w", stdout);
#endif
int cas=;
while(~scanf("%s",s)){
trie.init();
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",tmp);
trie.insert(tmp);
}
trie.query(s);
printf("Case %d: %lld\n",cas++,dp[]);
}
return ;
}
UVA - 1401 | LA 3942 - Remember the Word(dp+trie)的更多相关文章
- Remember the Word UVALive - 3942(dp+trie)
题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...
- UVa 1025 A Spy in the Metro (DP动态规划)
题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...
- PHP:导出数据到word(包含图片)
1.方法 public function word() { $xlsModel = M('api_aliucheng'); $Data = $xlsModel->Field('id,u_name ...
- 取数字(dp优化)
取数字(dp优化) 给定n个整数\(a_i\),你需要从中选取若干个数,使得它们的和是m的倍数.问有多少种方案.有多个询问,每次询问一个的m对应的答案. \(1\le n\le 200000,1\le ...
- 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)
洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...
- [Codeforces722E] Research Rover (dp+组合数学)
[Codeforces722E] Research Rover (dp+组合数学) 题面 给出一个N*M的方格阵,从(1,1)出发,到(N,M)结束,从(x,y)只能走到(x+1,y)或(x,y+1) ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
随机推荐
- Github Desktop 克隆新项目 Authentication failed. You may not have permission to access the repository or the repository may ha
原来:ssh://git@github.com/xxx.git 改成:https://git@github.com/xxx.git
- 百度APP爬虫
1.抓包 访问一个频道,Charles抓包,找到真实连接,一般返回json数据和网页中数据对应为真实连接 请求方式为post,所以要添加请求头和表单数据,由于在charles环境下,所以要添加char ...
- [复习]莫比乌斯反演,杜教筛,min_25筛
[复习]莫比乌斯反演,杜教筛,min_25筛 莫比乌斯反演 做题的时候的常用形式: \[\begin{aligned}g(n)&=\sum_{n|d}f(d)\\f(n)&=\sum_ ...
- C#解决方案生成工具(2)
环境 VS2017 社区版 W10 Project类 : 在Microsoft.Build.Evaluation命名空间下,可使用Project类分析项目的.csproj文件 // 实例化对象,参数 ...
- CSS解决字母不换行
通过百度,查找解决方案 1. word-break:break-all;只对英文起作用,以字母作为换行依据2. word-wrap:break-word; 只对英文起作用,以单词作为换行依据
- JavaScript ES6 核心功能一览
JavaScript 在过去几年里发生了很大的变化.这里介绍 12 个你马上就能用的新功能. JavaScript 历史 新的语言规范被称作 ECMAScript 6.也称为 ES6 或 ES2015 ...
- [LNOI2014]LCA(树链剖分+线段树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3626 题解:看到LCA,我们可以直接想到这题的正解不是LCA!(LCA只能得20分,还要 ...
- 第十五节、韦伯局部描述符(WLD,附源码)
纹理作为一种重要的视觉线索,是图像中普遍存在而又难以描述的特征,图像的纹理特征一般是指图像上地物重复排列造成的灰度值有规则的分布.纹理特征的关键在于纹理特征的提取方法.目前,用于纹理特征提取的方法有很 ...
- #ifndef HeaderName_h #define HeaderName_h #endif 使用详解(转)
原文:#ifndef HeaderName_h #define HeaderName_h #endif 使用详解 想必很多人都看到过头文件中写有:#ifndef HeaderName_h ...
- Gym 101911F “Tickets”
传送门 题意: 给你一个由六位数字组成的门票编码x,并定义F(x) = | 前三位加和 - 后三位加和|: 求出给定的门票编码 x 之前并且 F(i) < F(x) 的 i 的总个数. 题解: ...