Balanced Sequence(栈+贪心排序)[sort的排序]

本文介绍了一种通过重新排列和连接包含括号的字符串来构造最长平衡括号子序列的方法。通过对每个输入字符串进行预处理并按特定条件排序,算法能够高效地找到所有可能组合中最长的有效括号序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type is said to be balanced:

+ if it is the empty string
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced.

Chiaki can reorder the strings and then concatenate them get a new string t. Let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. Chiaki would like to know the maximum value of f(t) for all possible t.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤105) -- the number of strings.
Each of the next n lines contains a string si (1≤|si|≤105) consisting of `(' and `)'.
It is guaranteed that the sum of all |si| does not exceeds 5×106.

Output

For each test case, output an integer denoting the answer.

Sample Input

2 1 )()(()( 2 ) )(

Sample Output

4 2

#include<bits/stdc++.h>

#define lowbit(x) (x&(-x))
#define ls(x)  (x<<1)
#define rs(x)  (x<<1|1)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-a;i>=a;i--)
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> PII;
const int mod=1e9+7;
const int maxn=1e5+10;

struct node{
    int l,r;
    int id;
}p[maxn];
char str[maxn],stk[maxn];


inline void solve(int& ans,int id){
    int top=-1;
    int len=strlen(str);
    rep(i,0,len){
        if(top>-1&&str[i]==')'&&stk[top]=='('){
            ans++;top--;
        }
        else{
                stk[++top]=str[i];
        }
    }
    p[id].l=0,p[id].r=0;
    rep(i,0,top+1){
        if(stk[i]==')')p[id].l++;
        else p[id].r++;
    }
    p[id].id=id;
}

bool cmp(const node& A,const node& B){
	//另一种写法
     if(min(A.r,B.l)==min(A.l,B.r)) return A.r>B.r;
     return min(A.r,B.l)>min(A.l,B.r);
/*
    if(A.l<=A.r&&B.l>B.r)
            return true;
    if(A.l>A.r&&B.l<=B.r)
            return false;
    if(A.l<=A.r&&B.l<=B.r)
        return A.l<B.l;
    return A.r>B.r;
*/
}

int main(){
   // freopen("1002.txt","r",stdin);
   // freopen("123.txt","w",stdout);
    int T;
    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++){
        int n;
        scanf("%d",&n);
        int ans=0;
        rep(i,1,n+1){
            scanf(" %s",str);
            solve(ans,i);
        }
        sort(p+1,p+n+1,cmp);
        int num=0;
        rep(i,2,n+1){
            num+=p[i-1].r;
            if(p[i].l>=num){
                ans+=num;
                num=0;
            }
            else{
                ans+=p[i].l;
                num-=p[i].l;
            }
        }
        printf("%d\n",ans*2);
    }
    return 0;
}

sort排序,用于流水线调度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值