hdu 6299 Balanced Sequence (括号序列,贪心)

大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t).

 

 

首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解.

那么这样最终就为n个类似于))))((的括号序列, 然后贪心排序即可

 

#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <string.h>
#include <queue>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;

const int N = 5e6+10, INF = 0x3f3f3f3f;
char s[N];
struct _ {
    int l, r;
    bool operator < (const _& x) const {
        if (l>r&&x.l<=x.r) return 1;
        if (l<=r&&x.l>x.r) return 0;
        if (l<=r&&x.l<=x.r) return l>x.l;
        return r<x.r;
    }
} a[N];
void work() {
    int n;
    scanf("%d", &n);
    int ans = 0, ll = 0, rr = 0;
    REP(i,1,n) {
        scanf("%s", s+1);
        int sum = 0, L = 0, R = 0, m = strlen(s+1);
        REP(i,1,m) {
            if (s[i]==')') sum--;
            else sum++;
            if (sum<0) L-=sum,sum=0;
        }
        R = sum;
        ans += m-L-R;
        a[i].l=L, a[i].r=R;
    }
    sort(a+1,a+1+n);
    int L = 0;
    REP(i,1,n) {
        a[i].r = min(a[i].r,L);
        ans += a[i].r*2;
        L -= a[i].r;
        L += a[i].l;
    }
    printf("%d\n", ans);
}

int main() {
    int t;
    scanf("%d", &t);
    REP(i,1,t) work();
}

 

转载于:https://www.cnblogs.com/uid001/p/10441334.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值