UVALive - 3942 Remember the Word (Trie + DP)
题意:
给定一篇长度为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)的更多相关文章
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- UVA - 1401 Remember the Word(trie+dp)
1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 3942 Remember The Word (Tire)
状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...
- UvaLive3942(Trie + dp)
查了半天数组越界的RE,才发现自己把ch数组放结构体里是过大的……放全局就A了. 类似区间的dp比较显然,只是用trie树做了优化,使得可以在trie树里一边走一边往上加dp值,不必枚举以前的每个位置 ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
随机推荐
- k8s的nfs存储外挂设置过程
需求: 在k8s集群里的某个模块生成的目录文件或者更新的目录文件,存储到外面某台服务器上 1.安装nfs服务(192.168.1.2 Ubuntu 16.04) apt-get install nf ...
- SQLServer数据库表字段超长,找到超长字段脚本
平时开发系统时偶尔会遇到数据超长导致往数据库中保存时出错. 使用下边的脚本可以方便的找出超长的字段. 1.通过正式表创建临时表,修改临时表中varchar.nvarchar的长度为max ); ); ...
- 电脑Bois中usb模式启动热键
组装机主板 品牌笔记本 品牌台式机 主板品牌 启动按键 笔记本品牌 启动按键 台式机品牌 启动按键 华硕主板 F8 联想笔记本 F12 联想台式机 F12 技嘉主板 F12 宏基笔记本 F12 惠普台 ...
- 开源项目android-uitableview介绍
在iOS应用中,UITableView应该是使用率最高的视图之一了.iPod.时钟.日历.备忘录.Mail.天气.照片.电话.短信. Safari.App Store.iTunes.Game Cent ...
- JFrame Frame 窗口关闭
JFrame是Frame的子类,即JFrame继承自Frame. JFrame是javax.swing.JFrame的类,Frame是java.awt.Frame的类. 类似的还有JButton.Bu ...
- python基础---有关nparray----切片和索引(一)
Numpy最重要的一个特点就是其N维数组对象,即ndarray,该对象是一种快速而灵活的大数据集容器,实际开发中,我们可以利用这种数组对整块数据执行一些数学运算. 有关ndarray,我们就从最简单的 ...
- Intellij IDEA项目添加资源文件
添加了一个资源文件,但读取的时候出错了 prop.load(Config.class.getResourceAsStream("/resources/dbconfig.properties& ...
- robotframework介绍
1.测试用例使用文本文件(TXT或者TSV文件)保存,使用制表符分隔数据.可以方便的使用任何文本编辑器,或者EXCEL编辑测试用例.也可以使用HTML格式创建用例.2.测试用例中支持变量使用,可以使用 ...
- linux系统及服务安全(持续更新中)
linux安全 1.隐藏NGINX和PHP版本号 curl -I "http://www.xxx.com" //检测 nginx: http段加入server_tokens of ...
- “IIS7.5无法写入配置文件web.config”的解决方案
原因 用了非NTSF(如:FAT32或exFAT分区格式,导致无法设置安全选项,从而导致无法自动写入文件). 解决方案 先在默认网站配置好,然后把web.config复制到目标网站的根目录,这样就等于 ...