Bzoj 4922(字符串匹配新高度+DP)
来源:互联网 发布:seo教程网 编辑:程序博客网 时间:2024/05/28 18:43
题意:
现给定n个括号序列,你需要选择若干序列,将它们按一定的顺序从左往右拼接起来,得到一个合法的括号序列。计算可以得到的合法的括号序列的长度的最大值。
题解:
题解:
首先对于每个括号序列,把左边的左括号和右边的右括号对消,最后能得到一坨这样的东西:
))…))((…((
就是
然后考虑假如我们的子集选好了,我们要按照什么顺序拼接才能拼成一个合法的括号序列呢?
BZOJ3709
能拼必须满足当前左括号数
显然先拼
先考虑
然后
那么我们把所有的二元组按照上述顺序排序后,以左括号数作为空间跑一遍背包就行了
注意由于物品大小有正有负所以要讨论一下枚举顺序。。。
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<stack>#include<queue>#include<vector>#include<set>#include<map>#include<string>using namespace std;typedef long long ll;typedef pair<int,int>P;const int INF=0x3f3f3f3f;const ll INFF=0x3f3f3f3f3f3f3f3f;const double pi=acos(-1.0);const double eps=1e-9;char str[505];int f[305][305*305];struct node{ int x,y,len;}a[305],a1[305],a2[305];bool cmp1(node p1,node p2){ return p1.x<p2.x;}bool cmp2(node p1,node p2){ return p1.x+p1.y>p2.x+p2.y;}int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",str); int len=strlen(str); int now=0; a[i].x=0; for(int j=0;j<len;j++) { if(str[j]==')') now--; else now++; a[i].x=max(a[i].x,-1*now); } a[i].len=len; a[i].y=now; } int cnt1=0,cnt2=0; for(int i=1;i<=n;i++) { if(a[i].y>=0) a1[++cnt1]=a[i]; else a2[++cnt2]=a[i]; } //printf("%d%d",cnt1,cnt2); sort(a1+1,a1+cnt1+1,cmp1); sort(a2+1,a2+cnt2+1,cmp2); for(int i=1;i<=cnt2;i++) a1[i+cnt1]=a2[i]; int s=0; memset(f,0xef,sizeof(f)); f[0][0]=0; for(int i=1;i<=n;i++) { for(int j=0;j<=s;j++) { f[i][j]=f[i-1][j]; } for(int j=a1[i].x;j<=s;j++) { f[i][j+a1[i].y]=max(f[i][a1[i].y+j],f[i-1][j]+a1[i].len); } s+=a1[i].len; } printf("%d\n",f[n][0]); return 0;}
阅读全文
1 0
- Bzoj 4922(字符串匹配新高度+DP)
- bzoj 2121: 字符串游戏 (字符串dp)
- bzoj 2121 字符串游戏(字符串dp)
- BZOJ 1461 字符串匹配
- 【bzoj 2121】字符串游戏 (dp)
- DP-回文字符串匹配
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
- bzoj 1264: [AHOI2006]基因匹配Match (DP+树状数组)
- BZOJ 1264 Match 基因匹配 (dp 树状数组优化)
- bzoj 4922(背包dp)
- [dp]Search 字符串匹配问题
- BZOJ 1090 [SCOI 2003] 字符串折叠 (区间DP)
- bzoj 4565: [Haoi2016]字符合并 (字符串dp)
- bzoj 1055: [HAOI2008]玩具取名(字符串dp)
- 【bzoj 1055】[HAOI2008]玩具取名(字符串dp)
- bzoj 4713: 迷失的字符串 (树形DP+bitset)
- fzoj 1381 Regular Expressions(DP:字符串匹配加强版)
- UVALive 6697 - Homework Evaluation(dp,字符串匹配得分)
- 数学问题——概率法求π的近似值
- LeetCode
- 详解Java动态代理机制
- 架构师之路(二十一)程序员多关注行业内领先技术
- vue环境搭建(有热替换)
- Bzoj 4922(字符串匹配新高度+DP)
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) E. Rusty String(fft)
- 判断元素出栈、入栈顺序的合法性
- 多态,抽象作业
- 23种设计模式之——代理模式
- 快速排序 递归调用
- jni访问java的属性和方法
- N的N次方的最高位
- 类和对象