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( ...
随机推荐
- JavaWeb_tomcat设置默认应用
在tomcat的server.xml文件中设置默认应用. 在tomcat文件目录里面的conf/server.xml文件中,在<Engine>...</Engine>中再增加一 ...
- eos表结构总结说明
EOS6.0 WORKFLOW 表结构说明 流程定义表(WFProcessDefine) 名称 代码 描述 流程定义ID processDefID 主键 流程定义名称 processDefName 业 ...
- Git 分支管理 Feature分支 强行删除分支
软件开发中,总有无穷无尽的新的功能要不断添加进来. 添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了, 所以,每添加一个新功能,最好新建一个feature分支, 在上面开发,完成后 ...
- vue路由守卫
路由守卫 //路由进来之时 beforeRouteEnter(to, from, next) { console.log(this, 'beforeRouteEnter'); // undefined ...
- memcached 扩展安装(windows)
在脚本之家里下载扩展压缩包 https://www.jb51.net/softs/392873.html 安装扩展前得先安装memcached并启动 下载完扩展压缩包解压并找到对应自己php版本 复制 ...
- 3dmax 法线重置
从一个模型分离部位时,分离出的部分,面法线发生了混乱,左边原始模型,右边分离后 重置法线方法 对模型(比如对分离出的下半身)添加 EditoNormal修改器 选中模型部位 添加Edit Normal ...
- Pycharm 设置TextStyle
之前在脚本中选择了一个字符串, PyCharm会"高亮"所有相同的字符串, 但是我不满意这个"高亮"的颜色,因为和背景色太相似了,所以需要做一下操作,修改这个& ...
- openinstall渠道统计工具介绍
大家好,今天给大家介绍一下如何使用openinstall 来实现APP 渠道统计,做运营推广的朋友应该对渠道统计并不陌生,之前一般都是让技术的同事打渠道包方式进行渠道统计,而且只有安卓才能打渠道包.o ...
- 初识java线程(Thread)
<1>.概念问题 线程的状态:1.NEW : 没有start的线程 2.RUNNING :可运行线程,可能正在执行,也可能正在等待操作系统中的其他资源,比如cpu时间片 3.BlOCKED ...
- CodeForces - 500A-New Year Transportation(模拟)
New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, ...