思路

用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的更多相关文章

  1. UVA1401 Remember the Word 字典树维护dp

    题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...

  2. UVa1401 Remember the Word(DP+Trie树)

    题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...

  3. UVA1401 Remember the word DP+Trie

    问题描述 洛谷(有翻译) 题解 DP,设\(opt_i\)代表前\(i\)个字符方案数. Trie优化,刷表法. \(\mathrm{Code}\) #include<bits/stdc++.h ...

  4. 【UVA1401】Remember the Word Trie+dp

    题目大意:给定一个字符串和一个字符串集合,问从集合中选出若干个串组成给定母串的不同方案数. 题解:有些类似于背包问题.状态很好表示,为:\(dp[i]\) 表示母串前 i 个字符的不同方案数,因此,有 ...

  5. Word/Excel 在线预览

    前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...

  6. C#中5步完成word文档打印的方法

    在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选 ...

  7. C# 给word文档添加水印

    和PDF一样,在word中,水印也分为图片水印和文本水印,给文档添加图片水印可以使文档变得更为美观,更具有吸引力.文本水印则可以保护文档,提醒别人该文档是受版权保护的,不能随意抄袭.前面我分享了如何给 ...

  8. 获取打开的Word文档

    using Word = Microsoft.Office.Interop.Word; int _getApplicationErrorCount=0; bool _isMsOffice = true ...

  9. How to accept Track changes in Microsoft Word 2010?

    "Track changes" is wonderful and remarkable tool of Microsoft Word 2010. The feature allow ...

随机推荐

  1. 14:CSS3 渐变(gradient)与 过度(transition)、CSS3 的2D动画

    14:CSS3 渐变 CSS3 渐变(gradient)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使用图像来实现这些效果,现在通过使用 CSS3 的渐变(gradients)即可 ...

  2. 10.17小结:table.copy() 和 distinct 查询

    1. 当datatable 已存在于一个dataset中时,可以使用 ds.tables.add(dt.copy()) 来向dataset 中添加datatable; 2. 当datarow已存在于一 ...

  3. Listen error 错误和 limit of inotify watches was reached

    今天在生产环境中报错rails c中报了一个错误: FATAL: Listen error: unable to monitor directories for changes. Visit http ...

  4. C#实现WinForm禁止最大化、最小化、双击标题栏、双击图标等操作的方法

    from:http://www.jb51.net/article/71319.htm 本文实例讲述了C#实现WinForm禁止最大化.最小化.双击标题栏.双击图标等操作的方法.分享给大家供大家参考.具 ...

  5. Android开发入门经典【申明:来源于网络】

    Android开发入门经典[申明:来源于网络] 地址:http://wenku.baidu.com/view/6e7634050740be1e650e9a7b.html?re=view

  6. xilink 烧写flash

    no 右键

  7. Java加载dll或so库文件的路径 java.library.path

      1. Java的System.load 和 System.loadLibrary都可以用来加载库文件   2.例如你可以这样载入一个windows平台下JNI库文件: System.load(&q ...

  8. ARGB与RGB、RGBA的区别

    ARGB 是一种色彩模式,也就是RGB色彩模式附加上Alpha(透明度)通道,常见于32位位图的存储结构. RGB 色彩模式是工业界的一种颜色标准,是通过对红(R).绿(G).蓝(B)三个颜色通道的变 ...

  9. 合作开发工具——freeze和pipreqs

    以后在合作开发的过程中,难免会用到别人开发到一半或者将自己开发的项目交给别人,在转交项目的时候需要让别人知道本项目中用到了哪些模块,这时可以用到一条命令来帮助我们. pip3 freeze # 获取环 ...

  10. linux环境如何配置repo

    (1)下载repo mkdir ~/bin       curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo  ...