UVALive - 3942 (DP + Trie树)
给出一个长度不超过300000的字符串 S,然后给出 n 个长度不超过100的字符串。
如果字符串可以多次使用,用这 n 个字符串组成 S 的方法数是多少?

比如样例中,abcd = a + b + cd = ab + cd
dp[i] 表示用这n个字符串构成,S中从 i ~ len之间的字母构成的子串,的可分解方案数。
如果存在一个位置 x >= i, 且 i~x 之间的字母是一个单词,那么dp[i] = ∑ ( dp[x] )
但是如果暴力枚举 i ~ x是不是一个单词,必然会TLE。这时我们就需要 Trie 树优化这个DP。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define maxn 400000 + 100
#define sigma_size 27
#define LL long long
#define MOD 20071027 int tot = ;
int trie[maxn][sigma_size], sum[maxn], dp[maxn]; void insert(char s[])
{
int root = ;
for (int i = ; s[i]; i++)
{
int id = s[i]-'a';
if (!trie[root][id])
{
sum[++tot] = ;
trie[root][id] = tot;
}
root = trie[root][id];
}
sum[root] = ;
} void found(char s[], int k)
{
int root = ;
for (int i = k; s[i]; i++)
{
int id = s[i]-'a';
if (!trie[root][id])
return;
root = trie[root][id];
if (sum[root])
dp[k] = (dp[k]+dp[i+])%MOD;
}
} int main()
{
char s[maxn];
int len, ca = ;
while(scanf("%s", s) != EOF)
{
char t[maxn];
int n;
scanf("%d", &n); memset(trie, , sizeof(trie));
memset(sum, , sizeof(sum));
for (int i = ; i <= n; i++)
{
scanf("%s", t);
insert(t);
} memset(dp, , sizeof(dp)); len = strlen(s); dp[len] = ;
for (int i = len-; i >= ; i--)
found(s, i); printf("Case %d: %d\n", ++ca, dp[]);
}
}
UVALive - 3942 (DP + Trie树)的更多相关文章
- Codeforces 615C Running Track(DP + Trie树)
题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...
- UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...
- 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)
P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...
- HNU 13108 Just Another Knapsack Problem DP + Trie树优化
题意: 给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大.每个模式串不止能用一次. 思路: 多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在 ...
- Codeforces 633C Spy Syndrome 2(DP + Trie树)
题目大概说给一个加密的字符串,加密规则是把原文转化成小写字母,然后各个单词反转,最后去掉空格.现在给几个已知的单词,还原加密的字符串. 和UVa1401一个道理.. 用dp[i]表示加密字符前i个字符 ...
- UVALive - 3942 (字典树)
递推:$d(i) $表示从第$i$个字符开始到末尾的字符串(即后缀S[i...n])的分解方案数,则$d(i) = \sum {d(i + len(x))} $,其中字符串$x$代表S[i...n]的 ...
- Remember the Word UVALive - 3942 DP_字典树
每个小单词的长度都是小于等于100的,这是个重要的突破口. Code: #include <cstdio> #include <algorithm> #include < ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
随机推荐
- 解决win10下python3和python2共存pip的问题
经过在查阅网友的各种经验,发现仍然解决不了问题,python2和python3在win10下的安装就不再演示了,直接在python的官网下载就好,我机器上使用的是python2.7.15和python ...
- E. Karen and Supermarket
E. Karen and Supermarket time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- JSONModel 简单例子
// ProductModel.h // JSONModel // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All righ ...
- 【持续更新】Java 时间相关
直接上代码: import java.util.*; import java.text.SimpleDateFormat; public class HelloWorld { public stati ...
- Yii2.0数据库缓存依赖发布的使用理解
对于产品中经常需要生成一些缓存类的东西,比如系统基础配置,商品分类等,每次修改调整后都要手动进行缓存发布,是不是非常麻烦!这时候Yii2.0的缓存依赖发布就起到至关重要的作用了!现将主要的使用流程介绍 ...
- HTTP 错误 500.0 - Internal Server Error
最近在二次开发一个APS.NET网站,将网站部署到IIS后,输入:http://localhost/upload/ 时,报错“HTTP 错误 500.0 - Internal Server Error ...
- Windows环境中,通过Charles工具,抓取安卓手机、苹果手机中APP应用的http、https请求包信息
Windows环境中,通过Charles工具,抓取安卓手机.苹果手机中APP应用的http.https请求包信息1.抓取安卓手机中APP应用的http请求包信息1)在电脑上操作,查看Windows机器 ...
- 11gR2 Agent 简介
目的:本文会对oracle 11gR2 集群件(Grid Infrastructure,以下简称GI) 新特性 agent进行介绍,包括 agent的功能,常见的agent介绍,以及基本的诊断方法. ...
- noip模拟赛#45
T1:n<=1e6,求最小的区间包含(1,m)的所有数. =>双指针扫一遍即可 #include<cstdio> #include<cstring> #includ ...
- Aho-Corasick自动机
在模式匹配问题中,如果模板有很多个,KMP算法就不太适合了.因为每次查找一个模板.都要遍历整个文本串.可不可以只遍历一次文本串呢?可以,方法是把所有模板组成一个大的状态转移图(称为$Aho-Coras ...