UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串。
- 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树)的更多相关文章
- UVA1401 Remember the word DP+Trie
问题描述 洛谷(有翻译) 题解 DP,设\(opt_i\)代表前\(i\)个字符方案数. Trie优化,刷表法. \(\mathrm{Code}\) #include<bits/stdc++.h ...
- Codeforces 615C Running Track(DP + Trie树)
题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...
- 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)
P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- UVALive - 3942 (DP + Trie树)
给出一个长度不超过300000的字符串 S,然后给出 n 个长度不超过100的字符串. 如果字符串可以多次使用,用这 n 个字符串组成 S 的方法数是多少? 比如样例中,abcd = a + b + ...
- HNU 13108 Just Another Knapsack Problem DP + Trie树优化
题意: 给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大.每个模式串不止能用一次. 思路: 多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在 ...
- Codeforces 633C Spy Syndrome 2(DP + Trie树)
题目大概说给一个加密的字符串,加密规则是把原文转化成小写字母,然后各个单词反转,最后去掉空格.现在给几个已知的单词,还原加密的字符串. 和UVa1401一个道理.. 用dp[i]表示加密字符前i个字符 ...
- Remember the Word,LA3942(Trie树+DP)
Trie树基础题,记录下代码. #include <cstdio> #include <cstring> #define MaxNode 4005*100 #define No ...
- NBUT 1222 English Game(trie树+DP)
[1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 问题描写叙述 This English game is a simple English words ...
随机推荐
- OpenCV入门(一)
参考:http://blog.csdn.net/poem_qianmo/article/details/20537737 这位同学挺牛的,才研一就出书了,实在是让人汗颜啊,不说了,多学习. 这一篇主要 ...
- HDOJ 2955 Robberies (01背包)
10397780 2014-03-26 00:13:51 Accepted 2955 46MS 480K 676 B C++ 泽泽 http://acm.hdu.edu.cn/showproblem. ...
- HDOJ 1596
9899828 2013-12-27 16:42:37 Accepted 1596 3312MS 6668K 711 B C++ 泽泽 floyed暴力 #include<cstdio> ...
- js中document.documentElement 和document.body 以及其属性 clientWidth等
在设计页面时可能经常会用到固定层的位置,这就需要获取一些html对象的坐标以更灵活的设置目标层的坐标,这里可能就会用到document .body.scrollTop等属性,但是此属性在xhtml标准 ...
- Objective-C中的instancetype和id区别
目录(?)[-] 有一个相同两个不同相同 Written by Mattt Thompson on Dec 10th 2012 一什么是instancetype 二关联返回类型related resu ...
- 通过JS语句判断WEB网站的访问端是电脑还是手机
通过JS语句判断WEB网站的访问端是电脑还是手机,以显示不同的页面! 目录腾讯网的适配代码如何判断访问网站的机器类型-如何判断ipadJS 判断浏览器客户端类型(ipad,iphone,android ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 14.python笔记之paramiko
作者:刘耀 博客:www.liuyao.me 博客园:www.cnblogs.com/liu-yao 转载请注明 一,介绍 1.使用paramiko可以很好的解决以上问题,比起前面的方法,它仅需要在本 ...
- Android 设置旋转朝向
在AndroidMenifest.xml中activity标签中,添加一个属性 android:screenOrientation="landscape"
- Fedora 21 install chrome
Steps to install Google Chrome on Fedora 21 A. Create google-chrome.repo file Use this command to cr ...