题意:

  给你一个文本串,和一些模式串,每个模式串都有一个价值,让你选一些模式串来组成文本串,使获得的价值最大。每个模式串不止能用一次。

思路:

  多重背包,枚举文本串的每个位置和模式串,把该模式串拼接在当前位置,看下一个位置是否能得到更优值。但是,存在很多模式串不能拼在当前位置的,无效状态。所以可以用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树优化的更多相关文章

  1. UVALive - 3942 (DP + Trie树)

    给出一个长度不超过300000的字符串 S,然后给出 n 个长度不超过100的字符串. 如果字符串可以多次使用,用这 n 个字符串组成 S 的方法数是多少? 比如样例中,abcd = a + b + ...

  2. FZU-2214 Knapsack problem(DP使用)

    Problem 2214 Knapsack problem Accept: 863    Submit: 3347Time Limit: 3000 mSec    Memory Limit : 327 ...

  3. Codeforces 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  4. UVa1401 Remember the Word(DP+Trie树)

    题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...

  5. 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)

    P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...

  6. CF1625D - Binary Spiders[trie树优化dp]

    官方题解 题意:给数列a[],选择尽量多的数满足任意两个异或起来<=k 1625D - Binary Spiders 思路:首先,将数列排序得到,然后升序取得的值的任意两个最小值为相邻两个异或的 ...

  7. [USACO2005][POJ3171]Cleaning Shifts(DP+线段树优化)

    题目:http://poj.org/problem?id=3171 题意:给你n个区间[a,b],每个区间都有一个费用c,要你用最小的费用覆盖区间[M,E] 分析:经典的区间覆盖问题,百度可以搜到这个 ...

  8. 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 ...

  9. UVA-1322 Minimizing Maximizer (DP+线段树优化)

    题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...

随机推荐

  1. C语言基本语法——字符串

    1.什么是字符串 2.字符串与普通字符数组的区别 3.字符串的定义方式 4.字符串的使用 5.什么是字符串数组 6.字符串数组的赋值 7.字符串数组的遍历 1.什么是字符串 • 用双引号引起来的多个字 ...

  2. python中return和print的区别(详细)

    huskiesir最近在研究python哈,今天纠结一个问题,那就是return和print的区别,都是可以输出结果的,到底有啥区别呀?二话不多说,看下面的例子. #代码1: def break_wo ...

  3. elementUI 日期时间选择器el-date-picker开始时间与结束时间约束

    主要思路:el-date-picker组件需要 :picker-options属性,属性值为data,data的数据来自于methods中的方法. ##template代码 <el-form-i ...

  4. js 学习思维导图

  5. 关于C++的一些函数的使用方法

    关于C++的一些函数的使用方法: http://www.shouce.ren/api/c/index.html

  6. 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

  7. java string遇到的一个奇葩bug

    String abc = "1"; HashMap<String, String> hMap = new HashMap<String, String>() ...

  8. html5中调用摄像头拍照

    方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象.调用摄像头的參数 PopPosition: JSON对象,弹出拍照或摄像 ...

  9. duang!!!为什么函数能够返回unique_ptr

    C++虐我千百遍,我待C++如初恋 从智能指针说起 对高手而言.指针是上天入地的神器.对新手而言,那简直是灾难的源泉.高级语言如Java,C#都自己主动管理内存.你仅仅管new.不必担心内存释放问题. ...

  10. myeclipse配置内存

    1.javaee项目假设耗费的内存过大,须要配置内存大小: 下图是配置tomcat结果:Optional program arguments: -Xms512M -Xmx512M -XX:PermSi ...