发现字典里面的单词数目多且长度短,可以用字典树保存

f[i]表示s[i~L]的分割方式,则有f[i]=∑f[i+len(word[j])]   其中word[j]为s[i~L]的前缀

注意字典树又叫前缀树,所以用前缀更方便,否则按顺序dp的话就要把字符倒序了

复杂度O(L*l) L为字符串长度,l为单词长度

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 300005
#define MOD 20071027
using namespace std;
struct Trie{
Trie* nxt[];
int v;
Trie(){
for(int i=;i<;i++){
nxt[i]=NULL;
}
v=;
}
};
Trie *root;
void insert(char s[],int vl){
Trie *p=root;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
if(p->nxt[t]){
p=p->nxt[t];
}
else{
p->nxt[t]=new Trie;
p=p->nxt[t];
}
}
p->v=vl;
}
int f[MAXN];
char s[MAXN];
int n,L;
int find(int st){
int ret=;
Trie *p=root;
for(int i=st;i<L;i++){
int t=s[i]-;
if(p->nxt[t]){
p=p->nxt[t];
if(p->v){
(ret+=f[i+])%=MOD;
}
}
else{
return ret;
}
}
return ret;
}
void del(Trie *p){
if(p){
for(int i=;i<;i++){
if(p->nxt[i]){
del(p->nxt[i]);
}
}
delete p;
}
}
int main()
{
int T=;
while(~scanf("%s",s)){
memset(f,,sizeof(f));
del(root);
L=strlen(s);
root=new Trie;
scanf("%d",&n);
for(int i=;i<=n;i++){
char ch[]={};
scanf("%s",ch);
insert(ch,);
}
f[L]=;
for(int i=L-;i>=;i--){
f[i]=find(i);
}
printf("Case %d: %d\n",++T,f[]);
}
return ;
}

UVALive - 3942:Remember the Word的更多相关文章

  1. UVALive - 3942 Remember the Word

    input 字符串s  1<=len(s)<=300000 n 1<=n<=4000 word1 word2 ... wordn 1<=len(wordi)<=10 ...

  2. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

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

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

  4. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  5. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  6. 我的VSTO之路(四):深入介绍Word开发

    原文:我的VSTO之路(四):深入介绍Word开发 在上一篇文章中,我介绍了Word的对象模型和一些基本开发技巧.为了更好的介绍Word插件开发,我为本文制作了一个Word书签的增强版,具体功能是让用 ...

  7. UVALive - 3942 Remember the Word (Trie + DP)

    题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...

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

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

  9. (Trie) uvalive 3942 Remember the word

    题意:告诉你一个母串和子串,能用多少种不同的方案组合出母串. 思路:字典树(显然)+DP DP: dp[i]+=dp[j+1]  i<=j<=m-1,且i到j的字符串能在字典树中找到.也就 ...

随机推荐

  1. Beta敏捷冲刺每日报告——Day2

    1.情况简述 Beta阶段Scrum Meeting 敏捷开发起止时间 2017.11.2 00:00 -- 2017.11.3 00:00 讨论时间地点 2017.11.2 晚9:30,电话会议会议 ...

  2. python 特殊方法实例

    import collections from random import choice card = collections.namedtuple('Card',['rank','suit']) # ...

  3. .Net Core MongoDB 简单操作。

    一:MongoDB 简单操作类.这里引用了MongoDB.Driver. using MongoDB.Bson; using MongoDB.Driver; using System; using S ...

  4. web信息泄露注意事项

    1. 确保您的Web服务器不发送显示有关后端技术类型或版本信息的响应头. 2. 确保服务器打开的端口上运行的所有服务都不会显示有关其构建和版本的信息. 3. 确保所有目录的访问权限正确,保证不会让攻击 ...

  5. SecureCRT 7.3注册机激活

    SecureCRT是一款很好用的远程登陆管理工具 工具和注册机下载链接:http://pan.baidu.com/s/1jImWiMU 密码:0yox 以管理管运行keygen.exe(一定要以管理员 ...

  6. 你考虑清楚了吗就决定用 Bootstrap ?

    近年来,在前端项目中, Bootstrap 已经成为了一个非常受欢迎的工具. Bootstrap 的确有很多优点,然而,如果你的团队中恰好有一个专职的前端工程师.那我推荐你们不要使用 Bootstra ...

  7. kubernetes进阶(01)kubernetes的namespace

    一.Namespace概念 Namespace是对一组资源和对象的抽象集合,比如可以用来将系统内部的对象划分为不同的项目组或用户组. 常见的pods, services, replication co ...

  8. kubernetes入门(03)kubernetes的基本概念

    一.Pod 在Kubernetes集群中,Pod是创建.部署和调度的基本单位.一个Pod代表着集群中运行的一个进程,它内部封装了一个或多个应用的容器.在同一个Pod内部,多个容器共享存储.网络IP,以 ...

  9. Oracle 用户创建及权限设置

    1:创建临时表空间create temporary tablespace user_temp  tempfile 'D:\app\Administrator\oradata\ORACLE\xyrj_t ...

  10. redis数据库各种数据类型的增删改查命令

    redis的常用数据操作: redis是key-value的数据结构,每条数据都是一个键值对. 键的类型是字符串并且键不能重复. redis最基本数据类型是string 所以下面的操作可以省略引号&q ...