思路

用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. mybatis03--字段名和属性名不一致

    1.修改数据库中的字段 2.运行根据id查询所有的学生信息的测试方法会出现下面的异常 也就是说明 数据库中的字段没有个实体类中的属性名一致 3.修改StudentMapper.xml文件中的列名 4. ...

  2. 解决ssh出现"Write failed: Broken pipe"问题

    用 ssh 命令连接服务器之后,如果一段时间不操作,再次进入 Terminal 时会有一段时间没有响应,然后就出现错误提示: Write failed: Broken pipe 只能重新用 ssh 命 ...

  3. ajax里post 设置请求头的编码格式

    我们常用的ajax形式就是post和get.post需要设置请求头,那么问题来了: 首先,为什么get不需要设置编码格式? 其次:不设置post请求头编码格式可以吗? 还有:常用的请求头编码格式有哪些 ...

  4. python __all__

    它不仅在第一时间展现了模块的内容大纲,而且也更清晰的提供了外部访问接口. 若__all__的list中未定义,即便有实现也会找不到.

  5. socket与http

    参考文档:http://blog.csdn.net/zeng622peng/article/details/5546384 1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可 ...

  6. [httpd] httpd server 在低负载的情况下对SYN无响应

    如题: 两台client通过load balance访问httpd server.两个client交互访问.load balance处于fullnat模式. server在低负载情况下,常常对某一个c ...

  7. block,inline和inline-block概念和区别(转载)

    转自: http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-lev ...

  8. 小程序 showModal content换行

    wx.showModal({ title: '提示', content: '1.该拼团仅支持到指定取货地址自提\r\n2.拼团支付价格为拼团原价,当到达指定阶梯,拼团差价将在3个工作日内退回您的微信账 ...

  9. pandas apply 添加进度条

    Way:from tqdm import tqdmimport pandas as pdtqdm.pandas(desc='pandas bar')df['title_content'] = df.p ...

  10. android studio java: -source 1.6 中不支持 switch 中存在字符串 的错误

    F4打开open moudle setting 将source Compatibility 改为1.7 F4打开open moudle setting 将targetCompatibility 改为1 ...