递推:$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. requests库的基本使用

    1.发送get请求 import requests # response=requests.get('http://www.baidu.com') # 查看响应内容,返回的是已经解码的内容 # res ...

  2. 分享一段js,判断是否是在iPhone中的Safari浏览器打开的页面

    头部引用jquery包 将下面的一段js写在</body>的前面 <script type="text/javascript"> var ua = navi ...

  3. JavaWeb初级进阶高级学习方向计划

    阶段1 语言基础 课程一.Java负基础扫盲课 1.初识Java 本课程从java开发环境配置开始,讲解了java语法基础 .类和面向对象.面向对象编程三大特性:封装.继承.多态.建议零基础学员从本课 ...

  4. apache----------在apache环境下安装https支持

    1.安装mod_ssl  yum install mod_ssl2.修改阿帕奇的配置文件开启3.防火墙要开启443端口4.要把三个证书上传到阿帕奇配置文件下.5.更新 httpd.conf 配置文件 ...

  5. caffe-ssd需要安装opencv

    https://blog.csdn.net/xiaxiazls/article/details/52039473

  6. Fastcgi、CGI 是什么

    1.CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. 2.web server(比如说nginx)只是内容的分发者. 比如,如果请求/index ...

  7. HashMap 源码分析

    static final int DEFAULT_INITIAL_CAPACITY = 16;  默认容量 static final int MAXIMUM_CAPACITY = 1073741824 ...

  8. 读HashMap 源码(jdk11)的见解

    如果想系统详细的了解HashMap请移步各大佬博客.这篇文章只是个人的一些见解. 数组+链表 或 数组+红黑树.这种说法感觉有迷惑性. 之前看博客都说 HashMap 的存储是数组+链表(jdk6), ...

  9. 【论文速读】XiangBai_TIP2018_TextBoxes++_A Single-Shot Oriented Scene Text Detector

    XiangBai_TIP2018_TextBoxes++_A Single-Shot Oriented Scene Text Detector 作者和代码 Minghui Liao, Baoguang ...

  10. OAuth授权 | 把这一篇丢给他

    OAuth授权 一.背景 上一篇我们介绍了单点登录(SSO),它能够实现多个系统的统一认证.今天我们来谈一谈近几年来非常流行的,大名鼎鼎的OAuth.它也能完成 统一认证,而且还能做更多的事情.至于O ...