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

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. python 使用Nginx和uWSGI来运行Python应用

    参考:http://zmrenwu.com/post/20/ uWSGI是一个Web应用服务器,它具有应用服务器,代理,进程管理及应用监控等功能.它支持WSGI协议,同时它也支持自有的uWSGI协议, ...

  2. nyoj VF

    VF 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Vasya is the beginning mathematician. He decided to make ...

  3. js 时间戳 vue 时间戳的转换 ?

    在没有用vue做项目之前 也遇到过戳转换的问题 直接函数 调用 方法 这个也可以写成vue的  把function去掉  formatDate后面加冒号 就可以了 当然这个不是原创 但是是谁的我忘记了 ...

  4. SSM框架中前端页面(AJAX+Jquery+spring mvc+bootstrap)

    前端新增页面的模态框,采用bootstarp建立.定义了empName,email,gender,depatName,四个属性的ID:其中保存按钮的ID:emp_save_btn,对应的点击函数如下: ...

  5. Python爬虫之urllib模块2

    Python爬虫之urllib模块2 本文来自网友投稿 作者:PG-55,一个待毕业待就业的二流大学生. 看了一下上一节的反馈,有些同学认为这个没什么意义,也有的同学觉得太简单,关于Beautiful ...

  6. JAVA 中一个非常轻量级只有 200k 左右的 RESTful 路由框架

    ICEREST 是一个非常轻量级只有 200k 左右的 RESTful 路由框架,通过 ICEREST 你可以处理 url 的解析,数据的封装, Json 的输出,和传统的方法融合,请求的参数便是方法 ...

  7. GIT入门笔记(10)- 多种撤销修改场景和对策

    场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file. 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步, ...

  8. SQL字符串操作汇总

    SQL字符串操作汇总 --将字符串中从某个字符开始截取一段字符,然后将另外一个字符串插入此处 select stuff('hello,world!',4,4,'****')   --返回值hel*** ...

  9. POJ1015 && UVA - 323 ~Jury Compromise(dp路径)

    In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of ...

  10. Python之socketserver模块和验证客户端链接的合法性

    验证客户端链接的合法性 分布式系统中实现一个简单的客户端链接认证功能 #_*_coding:utf-8_*_ from socket import * import hmac,os secret_ke ...