UVA1401 Remember the Word 字典树维护dp
题目链接:https://vjudge.net/problem/UVA-1401
题目:
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie. Since Jiejie can’t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie’s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks. The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.
Input
The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.
The second line contains an integer S, 1 ≤ S ≤ 4000. Each of the following S lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase. There is a blank line between consecutive test cases. You should proceed to the end of file.
Output
For each test case, output the number, as described above, from the task description modulo 20071027.
Sample Input
abcd
4
a
b
cd
ab
Sample Output
Case 1: 2
题意:
多组输入,首先给你一个长度最大为3e5的字符串s
然后给你一个整数n,后面给你n个长度最大为100的字符串str[i]
问你使用str组成s字符串有多少种方式
这里讲解一下样例:
abcd
4
a
b
cd
ab
那么abcd可以通过a+b+cd 或者 ab+cd 两种方式构成
题解:
dp方程很容易找到
dp[i]=(dp[i]+dp[j]) (i<j)
dp[i]表示构成s字符串的[i,len](这里我们把s字符串下标看作从1开始)这一个子串有多少种方式
那么我们就是需要找到有多少个j可以满足i的需求,因为如果dp[i]+=dp[j],那么s的子串[i,j-1]就需要是str字符串
中的一个才可以
那么暴力判断的话肯定就会TLE,这个时候我们使用字典树来维护
字典树建树的复杂度是O(n),n就是所有字符串的长度,在这里就是所有str字符串的长度,大致建树复杂度就是O(1e5)
另外在字典树上查找满足要求的j的时候,因为str最长为100,所以查找的复杂度最大也是100
那么所有复杂度就是O(1e5)+O(1e5*1e2)
代码:
/*
题意:
多组输入,首先给你一个长度最大为3e5的字符串s
然后给你一个整数n,后面给你n个长度最大为100的字符串str[i]
问你使用str组成s字符串有多少种方式 这里讲解一下样例:
abcd
4
a
b
cd
ab 那么abcd可以通过a+b+cd 或者 ab+cd 两种方式构成 题解:
dp方程很容易找到
dp[i]=(dp[i]+dp[j]) (i<j)
dp[i]表示构成s字符串的[i,len](这里我们把s字符串下标看作从1开始)这一个子串有多少种方式
那么我们就是需要找到有多少个j可以满足i的需求,因为如果dp[i]+=dp[j],那么s的子串[i,j-1]就需要是str字符串
中的一个才可以 那么暴力判断的话肯定就会TLE,这个时候我们使用字典树来维护
字典树建树的复杂度是O(n),n就是所有字符串的长度,在这里就是所有str字符串的长度,大致建树复杂度就是O(1e5)
另外在字典树上查找满足要求的j的时候,因为str最长为100,所以查找的复杂度最大也是100
那么所有复杂度就是O(1e5)+O(1e5*1e2) */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=3e5+10;
const int mod=20071027;
typedef struct Trie* TrieNode;
int dp[maxn],flag;
struct Trie
{
int sum;
TrieNode next[30];
Trie()
{
sum=0;
memset(next,NULL,sizeof(next));
}
};
void inserts(TrieNode root,char s[105])
{
TrieNode p = root;
int len=strlen(s);
for(int i=0; i<len; ++i)
{
int temp=s[i]-'a';
if(p->next[temp]==NULL) p->next[temp]=new struct Trie();
p=p->next[temp];
}
p->sum+=1;
}
void Del(TrieNode root)
{
for(int i=0 ; i<2 ; ++i)
{
if(root->next[i])Del(root->next[i]);
}
delete(root);
}
void query(TrieNode root,char s[105],int pos)
{
TrieNode p = root;
int len=strlen(s+1),ci=0;
for(int i=pos;i<=len;++i)
{
int temp=s[i]-'a';
if(p->next[temp]==NULL)
{
return;
}
else
{
p=p->next[temp];
}
ci++;
if(p->sum>0)
{
//printf("%d %d %d\n",pos,dp[pos],dp[pos+ci]);
dp[pos]+=dp[pos+ci];
dp[pos]%=mod;
}
}
}
char ss[maxn],str[105];
int main()
{
int n,p=0;
while(~scanf("%s",ss+1))
{
flag=0;
memset(dp,0,sizeof(dp));
TrieNode root = new struct Trie();
scanf("%d",&n);
for(int i=0 ; i<n; ++i)
{
scanf("%s",str);
inserts(root,str);
}
int len=strlen(ss+1);
dp[len+1]=1;
for(int i=len;i>=1;--i)
{
//printf("------------%d\n",i);
query(root,ss,i);
}
printf("Case %d: %d\n",++p,dp[1]);
Del(root);
}
return 0;
}
UVA1401 Remember the Word 字典树维护dp的更多相关文章
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 【HDU - 5845】Best Division(xor-trie、01字典树、dp)
BUPT2017 wintertraining(15) #7E 题意 把数组A划分为k个区间,每个区间不超过L长度,每一个区间异或和之和为S.现在求:S不超过X,区间个数的最大值. 且A是这样给你的: ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- 【8.26校内测试】【重构树求直径】【BFS模拟】【线段树维护DP】
题目性质比较显然,相同颜色联通块可以合并成一个点,重新建树后,发现相邻两个点的颜色一定是不一样的. 然后发现,对于一条链来说,每次把一个点反色,实际上使点数少了2个.如下图 而如果一条链上面有分支,也 ...
- 2019牛客暑期多校训练营(第二场)E 线段树维护dp转移矩阵
题意 给一个\(n\times m\)的01矩阵,1代表有墙,否则没有,每一步可以从\(b[i][j]\)走到\(b[i+1][j]\),\(b[i][j-1]\),\(b[i][j+1]\),有两种 ...
随机推荐
- 一文彻底理解IO多路复用
在讲解IO多路复用之前,我们需要预习一下文件以及文件描述符. 什么是文件 程序员使用I/O最终都逃不过文件. 因为这篇同属于高性能.高并发系列,讲到高性能.高并发就离不开Linux/Unix,因此这里 ...
- 容器编排系统K8s之crd资源
前文我们了解了k8s节点污点和pod的对节点污点容忍度相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14255486.html:今天我们来聊一下扩展 ...
- leetcode-242有效字母异位词
题目 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram&quo ...
- 在 WPF 中使用 MahApps.Metro.IconPacks 提供的大量图标
MahApps.Metro.IconPacks https://github.com/MahApps/MahApps.Metro.IconPacks 提供了大量的高质量的图标供WPF使用,极其方便. ...
- wpf 中用 C# 代码创建 PropertyPath ,以对间接目标进行 Storyboard 动画.
如图,一个 Rectangle 一个 Button ,点击按钮时要通过动画完成对 Rectangle填充色的渐变动画. Xaml: 1 <Window 2 x:Class="WpfAp ...
- 容器编排系统K8s之Prometheus监控系统+Grafana部署
前文我们聊到了k8s的apiservice资源结合自定义apiserver扩展原生apiserver功能的相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/ ...
- Kafka底层原理剖析(近万字建议收藏)
Kafka 简介 Apache Kafka 是一个分布式发布-订阅消息系统.是大数据领域消息队列中唯一的王者.最初由 linkedin 公司使用 scala 语言开发,在2010年贡献给了Apache ...
- 【Spring】 Spring的核心容器
Spring的核心容器 文章目录 Spring的核心容器 BeanFactory ApplicationContext 1.通过ClassPathXmlApplicationContext创建 2.通 ...
- 【MySQL】一台服务器上搭建两个mysql节点
环境: CentOS 6.8 memory:1G Mysql 5.7 二进制安装包 1.安装相关的环境包 yum -y install gcc glibc libaio libstdc++ libs ...
- MySQL库和表的操作
MySQL库和表的操作 库操作 创建库 1.1 语法 CREATE DATABASE 数据库名 charset utf8; 1.2 数据库命名规则 可以由字母.数字.下划线.@.#.$ 区分大小写 唯 ...