HNU 13108 Just Another Knapsack Problem DP + Trie树优化
题意:
给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大。每个模式串不止能用一次。
思路:
多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在当前位置,看下一个位置是否能得到更优值。但是,存在很多模式串不能拼在当前位置的,无效状态。所以可以用Trie树来优化转移。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; const int INF = <<;
const int MAXN = 1e5+;
const int MAXLEN = ;
const int MAXNODE = 1e5+;; struct Trie {
int ch[MAXNODE][];
int val[MAXNODE], use; inline void init() {
for (int i = ; i < ; i++) ch[][i] = ;
val[] = ;
use = ;
}
inline int New() {
for (int i = ; i < ; i++) ch[use][i] = ;
val[use] = ;
return use++;
}
inline int idx(char c) {
return c - 'a';
}
void insert(char str[], int v) {
int len = strlen(str);
int p = ;
for (int i = ; i < len; i++) {
int c = idx(str[i]);
if (!ch[p][c]) ch[p][c] = New();
p = ch[p][c];
}
val[p] = max(val[p], v);
}
int solve(char S[], int dp[]) {
int len = strlen(S);
for (int i = ; i <= len; i++) dp[i] = -;
dp[] = ;
for (int i = ; i < len; i++) {
if (dp[i]<) continue;
for (int j = i, p = ; j <= len; j++) {
int c = idx(S[j]);
if (val[p]) dp[j] = max(dp[j], dp[i]+val[p]);
p = ch[p][c];
if (p==) break;
}
}
return max(dp[len], );
}
}; Trie solver; char S[MAXN], P[MAXLEN];
int dp[MAXN];
int m, v; int main() {
#ifdef Phantom01
freopen("HNU13108.txt", "r", stdin);
#endif //Phantom01 while (scanf("%s", S)!=EOF) {
scanf("%d", &m);
solver.init();
for (int i = ; i < m; i++) {
scanf("%s%d", P, &v);
solver.insert(P, v);
}
printf("%d\n", solver.solve(S, dp));
} return ;
}
HNU 13108 Just Another Knapsack Problem DP + Trie树优化的更多相关文章
- UVALive - 3942 (DP + Trie树)
给出一个长度不超过300000的字符串 S,然后给出 n 个长度不超过100的字符串. 如果字符串可以多次使用,用这 n 个字符串组成 S 的方法数是多少? 比如样例中,abcd = a + b + ...
- FZU-2214 Knapsack problem(DP使用)
Problem 2214 Knapsack problem Accept: 863 Submit: 3347Time Limit: 3000 mSec Memory Limit : 327 ...
- Codeforces 615C Running Track(DP + Trie树)
题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...
- UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...
- 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)
P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...
- CF1625D - Binary Spiders[trie树优化dp]
官方题解 题意:给数列a[],选择尽量多的数满足任意两个异或起来<=k 1625D - Binary Spiders 思路:首先,将数列排序得到,然后升序取得的值的任意两个最小值为相邻两个异或的 ...
- [USACO2005][POJ3171]Cleaning Shifts(DP+线段树优化)
题目:http://poj.org/problem?id=3171 题意:给你n个区间[a,b],每个区间都有一个费用c,要你用最小的费用覆盖区间[M,E] 分析:经典的区间覆盖问题,百度可以搜到这个 ...
- HDU4719-Oh My Holy FFF(DP线段树优化)
Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) T ...
- UVA-1322 Minimizing Maximizer (DP+线段树优化)
题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...
随机推荐
- 【WPF】这可能是全网最全的拖拽实现方法的总结
原文地址 https://www.cnblogs.com/younShieh/p/10811456.html 前文 本文只对笔者学习掌握的一般的拖动问题的实现方法进行整理和讨论,包括窗口.控件等内容的 ...
- webstorm注册码注册
最近jetbrains公司更新软件总是特别的快,很多私人搭建的server服务也失效了,现出一个能够永久破解的方法,非常简单,只需要2步就完成. 第一步 复制下方activation code,直接粘 ...
- 【转】C语言将字符串转换成对应的数字(十进制、十六进制)
转自:http://wawlian.iteye.com/blog/1315133 1.一个十进制数字的字符串表示转换成对应的整数.举例:将“1234”转换成整数1234 /*将字符串s转换成相应的整数 ...
- OO第二单元总结——电梯调度问题
一.设计策略. 在三次作业中,多线程程序的实现分以下几个步骤: 1. 主线程Main类的创建多个线程. 2. 共享对象的synchronized锁保证线程之间的互斥访问. 3. 采用notifyAll ...
- windows 64位上oracle 11g安装
每次下载安装都记不住,所以我总结一下,站在前人的肩膀上 原文地址:http://jingyan.baidu.com/article/48b558e33af4a57f39c09a42.html Orac ...
- 【codeforces 234F】Fence
[题目链接]:http://codeforces.com/problemset/problem/234/F [题意] 你有n块板要凃油漆; 然后每块板有高度h[i];(宽度都为1) 然后每块板只能凃同 ...
- C#窗体间的跳转传值
1.开发平台VS2012 2.需求:从一个窗体跳转到另一个窗体,并传递参数,接收返回值. 3.案列如图: 4.代码如下: 登陆窗体: //当点击注册按钮 private void button2_Cl ...
- CODEVS——T 1005 生日礼物
http://codevs.cn/problem/1005/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description ...
- spring-boot-maven-plugin 插件的作用(转)
OM 文件中添加了“org.springframework.boot:spring-boot-maven-plugin”插件.在添加了该插件之后,当运行“mvn package”进行打包时,会打包成一 ...
- 纯文本中识别URI地址并转换成HTML
问题 有一段纯文本text, 欲将其插入DOM节点div中. text中可能有超链接, 邮件地址等. 假设有, 识别之. 分析 假设仅仅是纯文本, 插入div中, 仅仅要将div.innerText设 ...