HDU-6299 Balanced Sequence(2018-HDU多校-第一场-02)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1695    Accepted Submission(s): 418

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

Source

2018 Multi-University Training Contest 1

题解:

首先我们对所有的字符串通过括号匹配找到串内正规括号子序列,之后剩下的串只有三种可能:
1. 只包含’(’
2. 先是一串’)’然后再是一串’(’
3. 只包含’)’

根据这三种可能可以分成三类:

1.‘(’比‘)’多的

2.‘(’和‘)’一样多的

3.‘(’比’)‘少的
然后,按照第一类,第二类,第三类的顺序放置串。然后对于一类串,我们按照’)’个数从小到大排。三类串的串,我们按照’(‘个数从大到小排。对于排序完的串,我们从前往后扫一遍就行了。

代码:

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5+50;

struct D{
    int l,r;//处理后左右括号的数量 
    int flag;//代表所属类 
    bool operator < (const struct D &b)const{
        if(flag == b.flag){
            if(flag < 0)return l > b.l;
            else if(flag >= 0) return r < b.r;
        }
        else return flag > b.flag;
    }
}board[MAXN];

char S[MAXN];
char Stack[MAXN];
int tot;

int Solve(int x){
    tot = 0;
    int sum = 0;
    int len = strlen(S);
    for(int i=0 ; i<len ; ++i){
        if(S[i] == '('){
            Stack[++tot] = S[i];
        }
        else if(tot && Stack[tot]=='('){
            --tot;
            sum += 2;
        }
        else Stack[++tot] = S[i];
    }
    board[x].l = board[x].r = 0;
    while(tot){
        if(Stack[tot] == '(')board[x].l++;
        else board[x].r++;
        --tot;
    }
    if(board[x].l > board[x].r)board[x].flag = 1;
    else if(board[x].l == board[x].r)board[x].flag = 0;
    else board[x].flag = -1;
    return sum;
}

int main(){
    
    int T,N;
    scanf("%d",&T);
    while(T--){
        int sum = 0;
        scanf("%d",&N);
        for(int i=1 ; i<=N ; ++i){
            scanf("%s",S);
            sum += Solve(i);
        }
        sort(board+1,board+1+N);
        int L = 0;
        for(int i=1 ; i<=N ; ++i){
        	int t = min(L,board[i].r);
        	L -= t;
            sum += 2*t;
            L += board[i].l;
        }
        printf("%d\n",sum);
    }
    
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值