题意:

给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章。

思路:

用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i]转移过来, 注意dp[L+1]要初始化为1.

递推写法

#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e5 + ;
const int mod = ;
char in[maxN];
int n;
struct Trie {
int ch[maxN][];
int val[maxN];
int sz;
void Init(){sz = ; memset(ch[], , sizeof(ch[])); memset(val, , sizeof(val));}
int idx(char c) {return c - 'a';}
void Insert(char *s){
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = n;
} }tree;
long long dp[maxN];
int main(){
// freopen("1.txt","r", stdin);
int kase = ;
while(~scanf("%s", in)){
tree.Init();
memset(dp , , sizeof(dp));
scanf("%d", &n);
char word[];
for(int i = ; i < n; i++){
scanf("%s", word);
tree.Insert(word);
}
int len = strlen(in);
dp[len] = ;
for(int pos = len ; pos >= ; pos--){
int u = ;
for(int i = pos, wordLen = ; i < len; i++, wordLen++){
int c = tree.idx(in[i]);
if(tree.ch[u][c] == ) break;
u = tree.ch[u][c];
if(tree.val[u] != ){
dp[pos] += dp[pos + wordLen];
dp[pos] %= mod;
}
}
}
printf("Case %d: %lld\n", kase++, dp[]);
} return ;
}

记忆化搜索

#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e6 + ;
const int mod = ;
char in[maxN];
int n;
struct Trie {
int ch[maxN][];
int val[maxN];
int sz;
void Init(){
sz = ;
memset(ch[], , sizeof(ch[]));
memset(val, , sizeof(val));
}
int idx(char c) {return c - 'a';}
void Insert(char *s){
int u = , n = strlen(s);
for(int i = ; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = n;
} }tree; long long dp[maxN];
int dfs(int pos){
int temp = pos;
if(dp[pos] > ) return dp[pos];
int ret = ;
int adr = ;
while(tree.ch[adr][in[pos] - 'a']){
adr = tree.ch[adr][in[pos] - 'a'];
if(tree.val[adr] != ){
ret = (ret + dfs(pos + )) % mod;
}
pos ++; }
dp[temp] = ret;
return ret;
} int main(){
freopen("1.txt","r", stdin);
int ncase = ;
while(~scanf("%s", in)){
tree.Init();
scanf("%d", &n);
char word[];
for(int i = ; i < n; i++){
scanf("%s", word);
tree.Insert(word);
} int l = strlen(in);
for(int i = ;i <= l; i ++)dp[i] = ;
dp[l] = ;
printf("Case %d: %d\n",ncase ++ , dfs());
} return ;
}

UVALive - 3942 Remember the Word (Trie + DP)的更多相关文章

  1. UVA 3942 Remember the Word (Trie+DP)题解

    思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...

  2. UVA - 1401 Remember the Word(trie+dp)

    1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...

  3. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  4. UVALive 3942 Remember the Word(字典树+DP)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. UVALive 3942 Remember The Word (Tire)

    状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...

  6. UvaLive3942(Trie + dp)

    查了半天数组越界的RE,才发现自己把ch数组放结构体里是过大的……放全局就A了. 类似区间的dp比较显然,只是用trie树做了优化,使得可以在trie树里一边走一边往上加dp值,不必枚举以前的每个位置 ...

  7. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  8. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  9. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

随机推荐

  1. Hadoop实战项目:小文件合并

    项目背景 在实际项目中,输入数据往往是由许多小文件组成,这里的小文件是指小于HDFS系统Block大小的文件(默认128M),早期的版本所定义的小文件是64M,这里的hadoop-2.2.0所定义的小 ...

  2. C#里边的控件缩写大全(比较规范)

    标准控件1 btn Button 2 chk CheckBox 3 ckl CheckedListBox 4 cmb ComboBox 5 dtp DateTimePicker 6 lbl Label ...

  3. Tomcat一

    Tomcat是如何处理http请求的 Tomcat有什么用? Tomcat是一个应用服务器,也是一个Servlet容器,用来接收前端传过来的请求,并将请求传给Servlet,并将Servlet的响应返 ...

  4. 帝国empirecms后台登陆次数限制修改

    打开文件:\e\config\config.php, 找到 'loginnum'=>5, 把5改为自己想要的数字即可

  5. Garmin APP开发之布局

    上一章节介绍了garmin app开发的入门,包括garmin-sdk,开发工具的安装部署,文章结尾我们新建了我们的第一个app程序Garmin开发-入门: http://tieba.baidu.co ...

  6. 从wireshark数据中分析rtmp协议,并提取出H264视频流

    我写的小工具 rtmp_parse.exe 使用用法如先介绍下: -sps  [文件路径] 解析 sps 数据 文件当中的内容就是纯方本的hexstring: 如 42 E0 33 8D 68 05 ...

  7. 投资20万研发的JFinal项目《旅游线路营销管理系统》准备公开课中

    18年初上线了一套旅游营销管理系统,目前给几个合作客户内测试用,是基于JFinal研发的一套旅游行业旅游线路批发零售系统(SAAS)版. 系统终端: PC后台管理分:总部.线路批发商.旅行社门店.个人 ...

  8. Elasticsearch-基本操作1

    Elasticsearch版本:6.0 一.文档 一个文档不仅包含数据,也包含元数据,三个必须的元数据如下 _index:具有共同特性分到一起的文档集合,标示了文档的存放位置: 名字小写,不以下划线开 ...

  9. dstat参数选项

    Usage: dstat [-afv] [options..] [delay [count]]Versatile tool for generating system resource statist ...

  10. mvc的help和functions语法

    @helper show(int num ) { ) { @:存在 } else { @:不存在 } } @functions { /// <summary> /// 方法必须要求为静态 ...