UVALive 3942 Remember The Word (Tire)
状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie。
每一次转移的复杂度是O(maxlen),maxlen是单词的最长长度。
#include<bits/stdc++.h>
using namespace std; const int maxn = *//4000*98+26*26+26+1
,sigma_size = ; int ch[maxn][sigma_size];
bool vis[maxn];
int nd_cnt; const int LEN = 3e5+, mod = ;
char str[LEN];
int d[LEN]; void init()
{
nd_cnt = ; memset(ch[],,sizeof(ch[]));
} void add(char *s)
{
int u = ;
int n = strlen(s);
for(int i = ; i < n; i++){
int c = word[i]-'a';
if(!ch[u][c]){
memset(ch[nd_cnt],,sizeof(ch[nd_cnt]));
vis[nd_cnt] = false;
ch[u][c] = nd_cnt++;
}
u = ch[u][c];
}
vis[u] = true;
} int main()
{
//freopen("in.txt","r",stdin);
char word[];
int kas = ;
while(gets(str)){
init();
int S; scanf("%d\n",&S);
while(S--){
gets(word);
add(word);
}
int n = strlen(str);
d[n] = ;
for(int cur = n-; cur >= ; cur--){
int u = ; d[cur] = ;
for(int i = cur; i < n; i++){
int c = str[i]-'a';
if(!ch[u][c]) break;
u = ch[u][c];
if(vis[u]) d[cur] += d[i+];
}
d[cur] %= mod;
}
printf("Case %d: %d\n",++kas,d[]);
}
return ;
}
UVALive 3942 Remember The Word (Tire)的更多相关文章
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive - 3942 Remember the Word (Trie + DP)
题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- C#操作Office.word(二)
在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- 最全总结 | 聊聊 Python 办公自动化之 Word(中)
1. 前言 上一篇文章,对 Word 写入数据的一些常见操作进行了总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 相比写入数据,读取数据同样很实用! 本篇文章,将谈谈如何全面读取 ...
- 最全总结 | 聊聊 Python 办公自动化之 Word(下)
1. 前言 关于 Word 文档的读写,前面两篇文章分别进行了一次全面的总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 最全总结 | 聊聊 Python 办公自动化之 Word( ...
随机推荐
- js上传文件到后台时序列化数据
let fd = new FormData() // 定义传递的序列化对象,for (let i = 0; i < addArr.length; i++) { // addArr是选中文件的输入 ...
- conntrack-tools使用
基础用法 系统配置 ### 开启流数据包统计(packets和bytes) # echo "net.netfilter.nf_conntrack_acct=1" >> ...
- svn git 的区别(别再问我了)
这篇主要是谈谈两者的区别,至于谁优谁劣看官自己思考吧! 把第一条理解到位思想到位了做起来才会有的放矢,其他几条都是用的时候才能体会到 1) 最核心的区别Git是分布式的,而Svn不是分布的.能理解这点 ...
- 51nod1069【Nim取石子游戏】
具体看:萌新笔记之Nim取石子游戏可以这么写: #include <bits/stdc++.h> using namespace std; typedef long long LL; in ...
- 洛谷P2188 小Z的 k 紧凑数
P2188 小Z的 k 紧凑数 题目描述 小 Z 在草稿纸上列出了很多数,他觉得相邻两位数字差的绝对值不超过 k 的整数特别奇特,称其为 k 紧凑数. 现在小 Z 想知道 [l,r] 内有多少个 k ...
- 使用Unity容器实现属性注入
简介 Unity :是微软用C#实现的轻量级,可扩展的依赖注入容器. 控制反转:(Inversion of Control,缩写为IoC),是用来消减程序之间的耦合问题,把程序中上层对下层依赖,转移到 ...
- 基于canvas绘图 缩放 做标记
技术要点: 1.img 绘制到canvas 2.绘制完成以后进行拖拽,缩放 3.使用canvas画图,在绘制的img上进行标记划线,当然可以实现跟过功能,例如百度地图的功能,做单个标记,区域标记等. ...
- IT兄弟连 JavaWeb教程 监听器3
监听域对象中属性变更的监听器 域对象中属性的变更的事件监听器就是用来监听ServletContext.HttpSession.HttpServletRequest这三个对象中的属性变更信息事件的监听器 ...
- TensorFlow高层封装:从入门到喷这本书
目录 TensorFlow高层封装:从入门到喷这本书 0. 写在前面 1. TensorFlow高层封装总览 2. Keras介绍 2.1 Keras基本用法 2.2 Keras高级用法 3. Est ...
- Java 多线程高并发编程 笔记(二)
1. 单例模式(在内存之中永远只有一个对象) 1.1 多线程安全单例模式——不使用同步锁 public class Singleton { private static Singleton sin=n ...