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 线段的右端点需要的最 ...
随机推荐
- 百度图标echarts.js的使用
echarts官网:http://echarts.baidu.com/api.html#echarts echarts是百度公司开的一种开源制作图片工具,是一个专门制作图表的开源工具,功能非常强大,地 ...
- 【技术翻译】SIFT算子原理及其实现 (一)介绍
介绍 匹配不同图片的特征是计算机视觉常见的问题. 当所有要匹配的图片很相似的时候(大小,方位),简单的角点检测算子就可以匹配,但是,当你的图片大小,方位不同的时候,你就要用到尺度不变特征变换(scal ...
- 安装idea
1.下载idea https://www.jetbrains.com/idea/download/#section=linux 2.解压 sudo tar -zxvf ideaIC-2018.3.2 ...
- [剑指offer] 5. 用两个栈实现队列+[剑指offer]30. 包含min函数的栈(等同于leetcode155) +[剑指offer]31.栈的压入、弹出序列 (队列 栈)
c++里面stack,queue的pop都是没有返回值的, vector的pop_back()也没有返回值. 思路: 队列是先进先出 , 在stack2里逆序放置stack1的元素,然后stack2. ...
- Django - 表与ORM操作
Django - 表与ORM操作 一. 模板语言 模板中也有自己的语言, 该语言可以实现数据展示 - {{ 变量 }} - 循环 {% for i in all_publisher %} {{ for ...
- How to check Open vSwitch version and supports OpenFlow version
Open vSwitch (OVS) is an open-source virtual switch, featuring programmable switch forwarding capabi ...
- Java并发和多线程1:并发框架基本示例
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括ThreadPool,Executor,Executors,ExecutorService,Com ...
- C语言使用memcpy函数实现两个数间任意位置的复制操作
c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中. 用法:void *memcpy(void *dest ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- Double 与 Float 的值的比較结果
首先看geeksforgeeks上的两个程序: 程序1: #include<stdio.h> int main() { float x = 0.1; if (x == 0.1) print ...