UVA1401 Remember the Word
思路
用trie树优化dp
设f[i]表示到第i个的方案数,则有\(f[i]=\sum_{x}f[i+len[x]]\)(x是s[i,n]的一个前缀),所以需要快速找出所有前缀,用Trie树即可
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MOD = 20071027;
int dp[301000],Trie[400100][26],Nodecnt=1,root=1,isword[400100],S,len[400100];
char s[301000],w[4011][110];
void insert(char *s,int ln,int inq){
int o=root;
for(int i=0;i<ln;i++){
if(!Trie[o][s[i]-'a'])
Trie[o][s[i]-'a']=++Nodecnt;
o=Trie[o][s[i]-'a'];
}
isword[o]=inq;
}
void query(char *s,int ln,int pos){
int o=root;
for(int i=0;i<ln&&o;i++){
o=Trie[o][s[i]-'a'];
if(isword[o])
dp[pos]=(dp[pos]+dp[pos+len[isword[o]]])%MOD;
}
}
void init(){
Nodecnt=1;
root=1;
memset(Trie,0,sizeof(Trie));
memset(isword,0,sizeof(isword));
memset(dp,0,sizeof(dp));
}
int main(){
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
int cnt=0;
while(~scanf("%s",s+1)){
++cnt;
init();
scanf("%d",&S);
for(int i=1;i<=S;i++){
scanf("%s",w[i]+1);
len[i]=strlen(w[i]+1);
insert(w[i]+1,len[i],i);
}
int lens=strlen(s+1);
dp[lens+1]=1;
for(int i=lens;i>=1;i--){
query(s+i,lens-i+1,i);
}
printf("Case %d: %d\n",cnt,dp[1]);
}
return 0;
}
UVA1401 Remember the Word的更多相关文章
- UVA1401 Remember the Word 字典树维护dp
题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...
- UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...
- UVA1401 Remember the word DP+Trie
问题描述 洛谷(有翻译) 题解 DP,设\(opt_i\)代表前\(i\)个字符方案数. Trie优化,刷表法. \(\mathrm{Code}\) #include<bits/stdc++.h ...
- 【UVA1401】Remember the Word Trie+dp
题目大意:给定一个字符串和一个字符串集合,问从集合中选出若干个串组成给定母串的不同方案数. 题解:有些类似于背包问题.状态很好表示,为:\(dp[i]\) 表示母串前 i 个字符的不同方案数,因此,有 ...
- Word/Excel 在线预览
前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...
- C#中5步完成word文档打印的方法
在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选 ...
- C# 给word文档添加水印
和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力.文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭.前面我分享了如何给 ...
- 获取打开的Word文档
using Word = Microsoft.Office.Interop.Word; int _getApplicationErrorCount=0; bool _isMsOffice = true ...
- How to accept Track changes in Microsoft Word 2010?
"Track changes" is wonderful and remarkable tool of Microsoft Word 2010. The feature allow ...
随机推荐
- 有关自动化构建gulp的搭建
1,cnpm instal bower -g 2, cnpm install bower 3, bower init 生成bower.json文件 4, type null >.bowerr ...
- Gym 101873K - You Are Fired - [贪心水题]
题目链接:http://codeforces.com/gym/101873/problem/K 题意: 现在给出 $n(1 \le n \le 1e4)$ 个员工,最多可以裁员 $k$ 人,名字为 $ ...
- python练习题-day11
1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), 要求:登录成功一次,后续的函数都无需再输入用户名和密码 flag=False def wrapper(fun): def inn ...
- SQLAlchemy(包含有Flask-Migrate知识点)
what's the SQLAlchemy SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQ ...
- GIT仓库如何恢复到前一次提交
GIT仓库如何恢复到前一次提交 通过使用Git版本恢复命令reset,可以回退版本.reset命令有3种方式: git reset –mixed:此为默认方式,不带任何参数的git reset,即 ...
- Linux命令之rpm安装命令
在 Linux 操作系统下,几乎所有的软件均通过RPM 进行安装.卸载及管理等操作.RPM 的全称为Redhat Package Manager ,是由Redhat 公司提出的,用于管理Linux 下 ...
- 分布式异步任务Celery
-A代表APP celery -A tasks worker --loglevel=info -n nodemaster -------------- celery@nodemaster v4.1.0 ...
- Centos安装Oracle及问题处理
安装Oracle前准备 创建运行oracle数据库的系统用户和用户组 [jonathan@localhost ~]$ su root #切换到root Password: [root@localhos ...
- 15.IEnumerable和IEnumerator
先说IEnumerable,我们每天用的foreach你真的懂它吗? 阅读目录 自己实现迭代器 yield的使用 怎样高性能的随机取IEnumerable中的值 我们先思考几个问题: 为什么在fore ...
- iPhoneX快速适配,简单到你想哭。
研究了5个小时的iPhoneX适配. 从catalog,storyboard,safearea等一系列文章中发现.如果我们想完全撑满全屏.那直接建一个storyboard就好了.但撑满全屏后,流海就是 ...