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 ...
随机推荐
- (转)网站DDOS攻击防护实战老男孩经验心得分享
网站DDOS攻击防护实战老男孩经验心得分享 原文:http://blog.51cto.com/oldboy/845349
- idea报错:Error running $classname: Command line is too long. Shorten command line for $classname.
Command line is too long 打印的变量太长了,超过了限制,这都会报错...我只想知道idea基于什么原理会报这个错... 解决 1.按照提示修改该类的配置,选择jar manif ...
- return void ajax
public class UserInfo { private String name; private Integer age; public String getName() { return n ...
- 死磕 java原子类之终结篇(面试题)
概览 原子操作是指不会被线程调度机制打断的操作,这种操作一旦开始,就一直运行到结束,中间不会有任何线程上下文切换. 原子操作可以是一个步骤,也可以是多个操作步骤,但是其顺序不可以被打乱,也不可以被切割 ...
- php 判断字符串中是否包含另一个字符串 strpos
strpos (PHP 4, PHP 5, PHP 7) strpos — 查找字符串首次出现的位置 说明 strpos ( string $haystack , $needle [, int $o ...
- 在jquery事件中修改Angular的model
HTML代码如下 <!DOCTYPE html> <html ng-app="qm"> <body> <input type=" ...
- redis自启动配置详解
一.概述 1.1原理 redis自启动的工作原理是怎么样的呢?Linux系统启动后,会有一个程序去特定目录下面扫描文件,然后执行这些文件,这些文件可称之为脚本.所以,你可以把你的工作写成一个脚本,放到 ...
- POJ 1845 Sumdiv (数学,乘法逆元)
题意: 给出数字A和B,要求AB的所有因子(包括AB和1)之和 mod 9901 的结果. 思路: 即使知道公式也得推算一阵子. 很容易知道,先把分解得到,那么得到,那么的所有因子之和的表达式如下: ...
- UVA 11925 Generating Permutations 生成排列 (序列)
题意:要用一个有序的序列生成给定序列,操作有两种,一是交换前两个元素,二是把第一个元素移动到最后去. 思路有两种: 1.映射,把给定序列映射成有序的序列,然后按照同样的替换规则把有序的序列映射掉,然后 ...
- 10.1 plan
1951 [Sdoi2010]古代猪文 Sdoi2010 Contest2 807 1928 1566 [NOI2009]管道取珠 806 1429 2756 [S ...