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 ...
随机推荐
- 浅谈Observer在代码中表现形式
说到观察者模式,基本在软件工程领域中是应用广泛,不知道的可以先学习一番,下面给个快速的回顾,然后在通过一个grpc中的responseObserver谈下观察者对象在代码中的位置. 喜欢类图,就不上其 ...
- meta标签大全(荐)
html的meta总结(常用) 1.Meta标签大全 <!-- 声明文档使用的字符编码 --> <meta charset='utf-8'> <!-- 优先使用 IE 最 ...
- Javascript实现MD5加密
/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...
- Nginx之使用nginx搭建简单的文件服务器
使用nginx可以搭建简单文件服务器 安装nginx(不详述) 修改配置文件 /usr/local/nginx/conf/nginx.conf user root; /usr/local/nginx/ ...
- ajax验证用户名是否被注册 ; ajax提交form表单
register.html 文件代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &quo ...
- Http协议Status状态代码
Http协议:Http协议(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,绝大多数的Web开发,都是构建在HTTP协议之上的Web应用http请求有三部分 ...
- freeswitch报错
1. 2010-10-26 11:01:58.448513 [ERR] sofia_reg.c:816 Can not do authorization without a compl ...
- C# Remoting例子
4-23 https://www.cnblogs.com/zhengyun_ustc/archive/2006/06/09/remoting_InvalidCredentialException.ht ...
- Oracle表空间迁移Move Tablespace
move一个表到另外一个表空间时,索引不会跟着一起move,而且会失效.(LOB类型例外) move分为: *普通表move *分区表move *LONG,LOB大字段类型move来进行测试和说明. ...
- docker mysql 数据库乱码
创建 mysql 时,需要加上编码,不然会乱码: docker run --name mysql01 -p : -e MYSQL_ROOT_PASSWORD=pwd123 -d mysql:5.5 - ...