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 ...
随机推荐
- react recompose
避免写嵌套 import { compose } from "recompose"; function Message(props) { const { classes } = p ...
- 【谈谈IO】BIO、NIO和AIO
BIO: BIO是阻塞IO,体现在一个线程调用IO的时候,会挂起等待,然后Thread会进入blocked状态:这样线程资源就会被闲置,造成资源浪费,通常一个系统线程数是有限的,而且,Thread进入 ...
- 【微信小程序——开发步骤1】
知识点: view,image,text编写文本框架 使用弹性盒子动态布局 使用rpx调试分辨率 在wxml中查看默认样式属性 步骤: 1.以如图页面实例说明如何写出微信文本内容 先对页面写出整体 ...
- 带你开发一款给Apk中自动注入代码工具icodetools(完善篇)【申明:来源于网络】
带你开发一款给Apk中自动注入代码工具icodetools(完善篇)[申明:来源于网络] 带你开发一款给Apk中自动注入代码工具icodetools(完善篇):http://blog.csdn.net ...
- php 延迟静态绑定: static关键字
abstract class DomainObject { public static function create() { return new self(); } } class User ex ...
- [math] sagemath
官网首页:http://www.sagemath.org 首页里引出的两个教程 http://www.gregorybard.com/Sage.html http://sagebook.gforge. ...
- 2、jeecg 笔记之 t:dictSelect 或 t:dgCol 自定义字典
1.需求 先说一下需求场景吧,我们知道 jeecg 中提供了下拉,其中包含两种使用场景: 一种是直接通过 t:dictSelect 使用,再就是 t:dgCol 用于表头的列表工具条标签: 总之就是 ...
- Hyper-v 虚拟机 关闭服务
1.Win + R 运行 2.输入 services.msc 回车 3.禁用下图的服务
- [macOS] Cannot find libz when install php56
After upgraded to 10.12 and xcode8.2, when updating php with homebrew, i got these errors: /usr/loca ...
- List查询重复值的个数,并根据重复的数目从多到少排列
package ttt; import java.nio.MappedByteBuffer; import java.util.ArrayList; import java.util.Collecti ...