HUNAN 11569 Just Another Knapsack Problem(AC自动机+dp)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11569&courseid=0
给出目标串,每个子串和对应的权值,然后要从子串中匹配出目标串并且权值最大.匹配的位置不能重复.
dp[i]为匹配到i这个位置时的最大价值,那么dp[i]=max(dp[i],dp[i-len[j]]+val[j]);
每次找到的匹配的子串在目标串的位置,然后动态转移。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout); #define Nn 510007
#define Mc 26 using namespace std; class Acautomaton
{
private: int chd[Nn][Mc];
int fail[Nn];
int ID[Mc];
int val[Nn];
int Q[Nn];
int sz;
int len[Nn]; public:
int dp[];
void Init()
{
fail[] = ;
for (int i = ; i < Mc; ++i)
{
ID[i] = i;
}
}
void Reset()
{
CL(chd[],);
sz = ;
}
int idx(char c) {return c-'a';}
void insert(char *s,int key)
{
int p = ;
for (; *s; s++)
{
int k = ID[*s - 'a'];
if (!chd[p][k])
{
CL(chd[sz],);
val[sz] = ;
len[sz]=;
chd[p][k] = sz++;
}
len[chd[p][k]]=len[p]+;
p = chd[p][k];
}
val[p] =max(val[p], key);
}
void Build()
{
int *s = Q ,*e = Q,i;
for (i = ; i < Mc; ++i)
{
if (chd[][i])
{
*e++ = chd[][i];
fail[chd[][i]] = ;
}
}
while (s != e)
{
int u = *s++;
for (i = ; i < Mc; ++i)
{
int &v = chd[u][i];
if (v)
{
*e++ = v;
fail[v] = chd[fail[u]][i];
}
else v = chd[fail[u]][i];
}
}
}
void find(char *T) {
int j=;
for(int i=,temp,c;T[i];i++) {
dp[i]=;
c=idx(T[i]);
while(j&&!chd[j][c]) j=fail[j];
j=chd[j][c];
temp=j;
while(temp) {
if(val[temp]) {
//printf("%d\n",val[temp]);
if(i-len[temp]<) //temp已经在目标串中出现,但是不能转移
dp[i]=max(dp[i],val[temp]);
else if(dp[i-len[temp]])
dp[i]=max(dp[i],dp[i-len[temp]]+val[temp]);
printf("%d %d %d\n",i,dp[i],len[temp]);
}
temp=fail[temp];
} }
}
/* int solve(char *s)
{
int p = 0;
int k,ans = 0;
for (; *s; s++)
{
k = ID[*s - 'a'];
while (!chd[p][k] && p != 0) p = fail[p]; p = chd[p][k]; int rt = p;
while (rt != 0 && val[rt] != -1)
{
ans += val[rt];
val[rt] = -1;
rt = fail[rt];
}
}
return ans;
}*/
}ac; char s[],t[]; int main()
{
Read();
int n,val,i;
while (~scanf("%s",s))
{
ac.Reset(); ac.Init();
scanf("%d",&n);
for (i = ; i < n; ++i){
scanf("%s %d",t,&val);
ac.insert(t,val);
}
ac.Build();
ac.find(s);
//scanf("%s",tr);
printf("%d\n",ac.dp[strlen(s)-]);
}
return ;
}
HUNAN 11569 Just Another Knapsack Problem(AC自动机+dp)的更多相关文章
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- bzoj 1030 [JSOI2007]文本生成器(AC自动机+DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1030 [题意] 给n个小串,随机构造一个长为m的大串,一个串合法当且仅当包含一个或多个 ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)
Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Oth ...
- 2021.11.11 P4052 [JSOI2007]文本生成器(AC自动机+DP)
2021.11.11 P4052 [JSOI2007]文本生成器(AC自动机+DP) https://www.luogu.com.cn/problem/P4052 题意: JSOI 交给队员 ZYX ...
- POJ1625 Censored!(AC自动机+DP)
题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ...
- HDU2296 Ring(AC自动机+DP)
题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...
- HDU2457 DNA repair(AC自动机+DP)
题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...
随机推荐
- Codeforces Round #271 (Div. 2)-B. Worms
http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ...
- 【Codeforces Rockethon 2014】Solutions
转载请注明出处:http://www.cnblogs.com/Delostik/p/3553114.html 目前已有[A B C D E] 例行吐槽:趴桌子上睡着了 [A. Genetic Engi ...
- IjkPlayer播放器秒开优化以及常用Option设置
https://blog.csdn.net/shareus/article/details/78585260 ijkplayer点播和直播视频 问题 解决及优化 https://blog.csdn. ...
- 746. Min Cost Climbing Stairs@python
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- HDU-1455-木棒
这题的话,我们,定义一个结构体,然后把木棒从大到小排序. 这些木棒如果是由多根等长木棒组成的,那目标长度一定大于等于其中最长的木棒长度,所这就是我们搜索的下限. 上限就是所有的木棒组成了一根木棒,就是 ...
- [LUOGU]P1443 马的遍历
题目描述 有一个n*m的棋盘(1< n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘的大小和马的坐标 输 ...
- [LUOGU] 1717 钓鱼
题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请他吃一次"年年大丰收",为了表示诚意,他还决定亲自去钓鱼,但是,因为 ...
- 【OS_Linux】Linux中虚拟机的三种上网方式——桥接、NAT、Host-only
1.桥接 桥接方便做实验,配置ip方便.可以和局域网中的其他机器进行通信,也可以和公网进行通信.缺点是会占用主机所在局域网的一个ip. 2. NAT NAT模式下虚拟机可以和主机进行通信,可以上网,而 ...
- php-7.0.16 , apache2.4.25 配置
官网下载php,apache 修改apache E:\php\Apache24\conf\httpd.conf Define SRVROOT "E:/php/Apache24" - ...
- invalid LOC header (bad signature)
[产生原因] 本地maven仓库相关jar存在问题. [解决方案] 删除本地maven相关jar并重新下载.