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( ...
随机推荐
- sqlserver2012——XML查询
1. CREATE TABLE STUDENT { S_ID INT, S_DATA xml } INSERT INTO STUDENT VALUES { 1, '<学生信息><姓名 ...
- Ext.apply(src,apply) 和 Ext.applyIf(src,apply)比较(转)
Ext.onReady(function(){ /* * Ext.apply(src,apply) 和 Ext.applyIf(src,apply) 两个方法的使用和区别比较 */ //Ext.app ...
- Listbox 实现Item双击事件
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) { int index = this.listBox1.IndexFro ...
- springboot 之 controller
添加一个testController的java 类,部分代码 注解标记这是一个controller,配置路径,自动加载配置. 注入的方式有@Autowired 和@Resource 二者的区别是 @A ...
- uoj#36. 【清华集训2014】玛里苟斯(线性基+概率期望)
传送门 为啥在我看来完全不知道为什么的在大佬们看来全都是显然-- 考虑\(k=1\)的情况,如果序列中有某一个\(a_j\)的第\(i\)位为\(1\),那么\(x\)的第\(i\)位为\(1\)的概 ...
- HTML5元素拖放设置总结
将元素图片放入div盒子内 1.首先设置元素为可拖放:在img标签内加入draggable=”true”. <img draggable="true"> 2.设置元素的 ...
- python爬虫——web前端基础(1)
1.HTML的基本结构 <html>内容</html>:HTML文档是由<html></html>包裹,这是HTML文档的文档标记,也称为HTML开始标 ...
- Huber鲁棒损失函数
在统计学习角度,Huber损失函数是一种使用鲁棒性回归的损失函数,它相比均方误差来说,它对异常值不敏感.常常被用于分类问题上. 下面先给出Huber函数的定义: 这个函数对于小的a值误差函数是二次的, ...
- Python面向对象之类和实例
1.类的定义 定义是用过class关键字 class Student(object): pass class 后面紧接着是类名,即Student,类名通常是大写开头的单词, 紧接着是(object), ...
- hadoop分布式存储(2)-hadoop的安装
总共分三步:1.准备linux环境 租用“云主机”,阿里云,unitedStack等,云主机不受本机性能影响(或者直接安转linux操作系统或者虚拟机也行): PuTTy Configuration ...