递推:$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. [elk]验证mapping字段数和数据字段数关系

    验证一个mapping下字段缺少或者超过 结论: 没有什么不可以. 1.如果数据字段不在mapping里,则动态会更新mapping. 2.数据字段数也可以小于mapping里字段数 创建一个mapp ...

  2. Spark SQL 编程初级实践

    一.实验目的 (1)       通过实验掌握 Spark SQL 的基本编程方法: (2)       熟悉 RDD 到 DataFrame 的转化方法: (3)       熟悉利用 Spark ...

  3. java的智能提示无法打开

    第一步:选中“window”->“preference”   第二步:选中“java”,并展开   第三步:选中“Editor”,并展开   第四步:选中“Content Assist”,在右侧 ...

  4. hybrid简单了解

    技术点总有它的来由. 文章概要: 1.hybrid 基本概念 2.前端和客户端的交互 3.前端和客户端的交互实现 4.前端交互实现关注点 5.小结 1.hybrid 基本概念 ⑴.什么是hybrid? ...

  5. CDI services--Decorators(装饰器)

    1.Decorators装饰器综述 拦截器是一种强大的方法在应用程序捕捉运行方法和解耦.拦截器可以拦截任何java类型的调用. 这使得拦截器适合解决事务管理,安全性,以及日记记录. 本质上说,拦截器并 ...

  6. Pyenv部署

    一.Git克隆方式 1.安装git yum -y install git 2.克隆pyenv到本地 git clone https://github.com/pyenv/pyenv.git ~/.py ...

  7. 重谈react优势——react技术栈回顾

    react刚刚推出的时候,讲react优势搜索结果是几十页. 现在,react已经慢慢退火,该用用react技术栈的已经使用上,填过多少坑,加过多少班,血泪控诉也不下千文. 今天,再谈一遍react优 ...

  8. MVC 纯Table实现树节点效果+授权

    这几夜心里颇不平静, 奈何 JS水平有限,前台效果耗时四天,后台传值一天,直至昨夜丑时测试初步完成,其实就是一个给tree来授权,网上开源的插件很多,如treeview.treejs.easyui 等 ...

  9. python垃圾回收机制与小整数池

    python垃圾回收机制 当引用计数为0时,python会删除这个值. 引用计数 x = 10 y = x del x print(y) 10 引用计数+1,引用计数+1,引用计数-1,此时引用计数为 ...

  10. An owner of this repository has limited the ability to open a pull request to users that are collaborators on this repository.

    git 无法发起:pull request,提示:An owner of this repository has limited the ability to open a pull request ...