题目给定一个字符串集合有几种方式拼成一个字符串。

  • dp[i]表示stri...strlen-1的方案数
  • dp[len]=1
  • dp[i]=∑dp[j](stri...strj-1∈SET)

用集合的字符串构造一棵Trie树,然后就可以在线性时间判断哪几个前缀字符串在集合里。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ch[][],tn;
bool flag[];
void insert(char *s){
int x=;
for(int i=; s[i]; ++i){
int y=s[i]-'a';
if(ch[x][y]==) ch[x][y]=++tn;
x=ch[x][y];
}
flag[x]=;
}
char word[];
int d[];
int main(){
int t=,n;
char str[];
while(~scanf("%s",word)){
tn=;
memset(ch,,sizeof(ch));
memset(flag,,sizeof(flag));
scanf("%d",&n);
while(n--){
scanf("%s",str);
insert(str);
}
memset(d,,sizeof(d));
int len=strlen(word);
d[len]=;
for(int i=len-; i>=; --i){
int x=;
for(int j=i; j<len; ++j){
int y=word[j]-'a';
if(ch[x][y]==) break;
x=ch[x][y];
if(flag[x]) d[i]+=d[j+],d[i]%=;
}
}
printf("Case %d: %d\n",++t,d[]);
}
return ;
}

UVa1401 Remember the Word(DP+Trie树)的更多相关文章

  1. UVA1401 Remember the word DP+Trie

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

  2. Codeforces 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  3. 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)

    P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...

  4. [POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)

    Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...

  5. UVALive - 3942 (DP + Trie树)

    给出一个长度不超过300000的字符串 S,然后给出 n 个长度不超过100的字符串. 如果字符串可以多次使用,用这 n 个字符串组成 S 的方法数是多少? 比如样例中,abcd = a + b + ...

  6. HNU 13108 Just Another Knapsack Problem DP + Trie树优化

    题意: 给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大.每个模式串不止能用一次. 思路: 多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在 ...

  7. Codeforces 633C Spy Syndrome 2(DP + Trie树)

    题目大概说给一个加密的字符串,加密规则是把原文转化成小写字母,然后各个单词反转,最后去掉空格.现在给几个已知的单词,还原加密的字符串. 和UVa1401一个道理.. 用dp[i]表示加密字符前i个字符 ...

  8. Remember the Word,LA3942(Trie树+DP)

    Trie树基础题,记录下代码. #include <cstdio> #include <cstring> #define MaxNode 4005*100 #define No ...

  9. NBUT 1222 English Game(trie树+DP)

    [1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 问题描写叙述 This English game is a simple English words ...

随机推荐

  1. Window 常用命令

    Window 常用命令   Ctrl +alt +delete 切换用户 Ctrl +shift+esc 启动任务管理

  2. Python ===if while for语句 以及一个小小网络爬虫实例

    if分支语句 >>> count=89 >>> if count==89: print count 89                          #单分支 ...

  3. 小白科普之JavaScript的函数

    一 概述 1.1 函数声明 (1)function命令 函数就是使用function命令命名的代码区块,便于反复调用.这种声明方式叫做函数的声明(Function Declaration). func ...

  4. 改变edittext边框颜色

    转载自:点击打开链接 第一步:为了更好的比较,准备两个一模一样的EditText(当Activity启动时,焦点会在第一个EditText上,如果你不希望这样只需要写一个高度和宽带为0的EditTex ...

  5. 关于LINUX文件与目录的问题说明

    文件权限一般可认为是0 123 456 789,一共十位: 0:表示该文件的文件类型.Windows里面是使用了一种文件关联的技术,通过扩展名来关联相应的应用程序,使得双击某个文件,就能达到调用相应的 ...

  6. 解决Centos关闭You have new mail in /var/spool/mail/root提示

    昨天搬到阿里云了. 装的系统是Centos 6.3的加固版 今天查看内存的时候 出现一天奇怪的提示 You have new mail in /var/spool/mail/root 有的时候每敲一下 ...

  7. SSDB 数据库如何换用 rocksdb 引擎?

     牧童遥指杏花村,一枝红杏出墙来… SSDB 数据库如何换用 rocksdb 引擎? idea's blog 2014-04-12 71 阅读 rocksdb NoSQL SSDB 数据库使用的是 G ...

  8. Backpack | & ||

    Backpack | Given n items with size Ai, an integer m denotes the size of a backpack. How full you can ...

  9. windows下的unix工具集:UnxUtils

    参考: http://blog.csdn.net/woohello/article/details/8365639 下载: http://sourceforge.net/projects/unxuti ...

  10. iOS的 context 和Android 中的 canvas

    ios 想要绘图,要用到CGContextRef类.最基本的用法是在- (void)drawRect:(CGRect)rect 函数中绘制. Android 中要用到Canvas类.最基本的用法是在  ...