递推:$d(i) $表示从第$i$个字符开始到末尾的字符串(即后缀S[i...n])的分解方案数,则$d(i) = \sum {d(i + len(x))} $,其中字符串$x$代表S[i...n]的前缀,且x可以和某个单词匹配。判断后缀串S[i...n]能和哪些单词匹配,使用字典树来实现O(100)复杂度判断。

AC代码

#include <stdio.h>
#include <string.h>
const int MOD = ;
const int maxnode = * + ;
const int sigma_size = ;
const int maxn = + ; char S[maxn], sb[ + ];
int d[maxn]; struct Trie {
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void clear() {
sz = ;
memset(ch[], , sizeof(ch[]));
}
int idx(char c) {
return c - 'a';
} void insert(char *s) {
int u = , n = strlen(s);
for(int i = ; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
val[sz] = ;
memset(ch[sz], , sizeof(ch[sz]));
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = n;
} void query_prefix(char *s, int i, int *d) {
int u = ;
for(int j = ; s[j] != '\0'; j++) {
int c = idx(s[j]);
if(!ch[u][c]) break;
u = ch[u][c];
if(val[u]) {
d[i] = (d[i] + d[i + val[u]]) % MOD;
}
}
}
}; Trie tree; int main() {
int m, kase = ;
while(scanf("%s", S) == ) {
tree.clear();
scanf("%d", &m);
for(int i = ; i < m; i++) {
scanf("%s", sb);
tree.insert(sb);
}
int n = strlen(S);
d[n] = ;
for(int i = n-; i >= ; i--) {
d[i] = ;
tree.query_prefix(S+i, i, d);
}
printf("Case %d: %d\n", kase++, d[]);
}
return ;
}

如有不当之处欢迎指出!

UVALive - 3942 (字典树)的更多相关文章

  1. UVALive 3942 字典树+dp

    其实主要是想学一下字典树的写法,但这个题目又涉及到了DP:这个题目要求某些单词组成一个长子串的各种组合总数,数据量大,单纯枚举复杂度高,首先肯定是要把各个单词给建成字典树,但是之后该怎么推一时没想到. ...

  2. UVALive 5029 字典树

    E - Encoded Barcodes Crawling in process...Crawling failedTime Limit:3000MS    Memory Limit:0KB    6 ...

  3. UVALive 5913 字典树

    先输入n个字符串的字典,每个字符串的前缀+后缀可以组成新的合法字符串,但肯定是有重复的,问从给定的字符串,生成的所有可能的字符串为多少个 把前缀和后缀压入字典树,达到前缀和后缀的去重,首先的总和即为前 ...

  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. UVALive 7712 Confusing Manuscript 字典树 查询与s的编辑距离为1的字符串数量

    /** 题目:UVALive 7712 Confusing Manuscript 链接:https://vjudge.net/problem/UVALive-7712 题意:给定n个不同的字符串,f( ...

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

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

  8. LA 3942 - Remember the Word (字典树 + dp)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  9. Trie(字典树)解析及其在编程竞赛中的典型应用举例

    摘要: 本文主要讲解了Trie的基本思想和原理,实现了几种常见的Trie构造方法,着重讲解Trie在编程竞赛中的一些典型应用. 什么是Trie? 如何构建一个Trie? Trie在编程竞赛中的典型应用 ...

随机推荐

  1. 爬取豆瓣电影排行top250

    功能描述V1.0: 爬取豆瓣电影排行top250 功能分析: 使用的库 1.time 2.json 3.requests 4.BuautifulSoup 5.RequestException 上机实验 ...

  2. oo第二次博客总结

    不知道怎么说好呢,自己对自己也很没有信心的,希望自己能做出些许改变

  3. asp.net的<% %>特定用法

    在asp.net应用程序中,在asp.net页面常用的<%@ %>.<%# %>.<%= %>.在全球化的项目中使用<%$ %>绑定资源项目,在asp. ...

  4. delphi 的一些注意点和知识点

    关于Delphi中产生的文件    编辑阶段: pas/单元文件,dpk/组件包文件,dpr/工程文件,dfm/窗体文件    编译阶段: dcu/单元编译文件,dcp/Delphi Compile ...

  5. [COCI11-12 #4]删数游戏(贪心+数据结构)

    题目描述 题目描述 给出一个N位数字串,删除任意K位,使剩下的数最大.. 输入 第1行:2个整数N和K(1<=K<=N<=500000) 第2行:N个数字(可能为0) 输出 第1行: ...

  6. Spring Boot 指定某个依赖的版本

    Spring Boot 是个很好的框架,他为了他的一些功能生效,定义了一些依赖的版本. 比如说:Spring Boot 1.5.x 中elasticSearch是2.4.x的,这个是他本身就定义好的. ...

  7. Navicat Premium 简体中文版 12.0.16 以上版本国外官网下载地址(非国内)

    国内Navicat网址是:http://www.navicat.com.cn 国外Navicat网址是:http://www.navicat.com 国外的更新比国内的快,而且同一个版本,国内和国外下 ...

  8. NI_NUMERICHOST" is not exported by the Socket module "getaddrinfo" is not expo

    [root@Server3 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf "NI_NUMERICHOST" is no ...

  9. CentOS7操作Redis4.0

    单机安装 1. 从官网下载 redis-4.0.10.tar.gz 到本地,然后上传到VMware虚拟机上,存放地址随意. 2. 解压: tar -zxvf redis-4.0.10.tar.gz 3 ...

  10. 关于IIS的4月26日笔记

    常用命令: 31. regedit.exe----注册表 48. msconfig.exe---系统配置实用程序  80. services.msc---本地服务设置 93. regedit.exe- ...