题目链接: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的更多相关文章

  1. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  3. 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 ...

  4. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  5. UVALive 3942 Remember the Word(字典树+DP)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. 【HDU - 5845】Best Division(xor-trie、01字典树、dp)

    BUPT2017 wintertraining(15) #7E 题意 把数组A划分为k个区间,每个区间不超过L长度,每一个区间异或和之和为S.现在求:S不超过X,区间个数的最大值. 且A是这样给你的: ...

  7. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  8. 【8.26校内测试】【重构树求直径】【BFS模拟】【线段树维护DP】

    题目性质比较显然,相同颜色联通块可以合并成一个点,重新建树后,发现相邻两个点的颜色一定是不一样的. 然后发现,对于一条链来说,每次把一个点反色,实际上使点数少了2个.如下图 而如果一条链上面有分支,也 ...

  9. 2019牛客暑期多校训练营(第二场)E 线段树维护dp转移矩阵

    题意 给一个\(n\times m\)的01矩阵,1代表有墙,否则没有,每一步可以从\(b[i][j]\)走到\(b[i+1][j]\),\(b[i][j-1]\),\(b[i][j+1]\),有两种 ...

随机推荐

  1. Go从入门到放弃(笔记存档)

    前言 考虑到印象笔记以后不续费了,这里转存到博客园一份 因内容是自己写的笔记, 未作任何润色, 所以看着很精简, 请见谅 查看官方文档 在新的go安装包中,为了减小体积默认去除了go doc 安装go ...

  2. java8新特性之stream流

    Stream 流是 Java 8 提供给开发者一套新的处理集合的API,他把我们将要处理的集合作为流,就像流水线一样,我们可以对其中的元素进行筛选,过滤,排序等中间操作,只不过这种操作更加简洁高效. ...

  3. SAP FTP FOR ABAP programing

    近来忙的不可开交,忙的一塌糊涂,呵呵,今天怀揣愧疚之心,上来分享博文一篇,算是对自己的一点安慰.   首先在SAP系统中提供了很多的FTP示例程序,如下: RSFTP001         SAPFT ...

  4. [Usaco2008 Open]Roads Around The Farm分岔路口

    题目描述 约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土地.她们将沿着一条路走,一直走到三岔路口(可以认为所有的路口都是这样的).这时候,这一群奶牛可能会分成两群,分别沿 ...

  5. win32 sdk 环境下创建状态栏

    今天在学习状态栏,出了好多的问题,这里记录下. 要创建状态栏用:CreateStatusWindow CreateStatusWindow函数创建一个状态窗口,通常用于显示应用程序的状态.窗口通常显示 ...

  6. 如何安装快速 Docker 和 Docker-Compose 服务

    最近由于个人在大家基于 Docker  的.企业级的CI/CD 环境,所以要安装 Docker 和 Docker-Compose ,这也算是一个学习过程,就把整个过程记录下来,便于以后查询. 测试环境 ...

  7. Py基础—变量名,条件循环,空执行,编码,运算符,字符比较,简化写法

    变量名 只能是字母,数字,下划线.数字不能开头,不要和python内置的东西重复.赋予变量名内容:name1 = "shit" 输出变量名内容 print(name1) 条件语句 ...

  8. 盼望着,盼望着。它终于来了!!!剪辑Windows PC测试版!

    盼望着,盼望着.它终于来了!!!剪辑Windows PC测试版! 喜欢短视频抖音的小伙伴们,是不是都对手机抖音剪映操作不是很满意.期待WINDOWS电脑版剪映的来临.在此之前只有MAC版本,不能满足广 ...

  9. 手把手做一个基于vue-cli的组件库(上篇)

    基于vue-cli4的ui组件库,先贴个最终效果吧,步骤有点多,准备分上下篇,上篇:如何做一个初步的组件.下篇:编写说明文档及页面优化.开工. GitHub源码地址:https://github.co ...

  10. Linux监控内核SNMP计数器

    nstat命令和rtacct命令是一个简单的监视内核的SNMP计数器和网络接口状态的实用工具. 语法 nstat/rtacct (选项) 选项 -h:显示帮助信息: -V:显示指令版本信息: -z:显 ...