HDU 6299 Balanced Sequence 括号匹配 贪心

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

题意:给出了n个序列,序列由“(”与")"组成,可以随意首尾拼接序列,求匹配后消除了多少个括号

思路:先把序列中本身匹配的记录下来,然后贪心处理,让左括号多的尽可能在左边,所以进行排序,左多右少的排在左少右多的前面,都是左多右少的话按照谁左括号多排序,都是左少右多的话,按照右括号少来排序。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
const int MAXN=1e5+5;
typedef long long ll;
struct node
{
    int l;
    int r;
    int num;
    int flag;//1 左多右少  0左少右多 -1一样多
}a[MAXN];
char s[MAXN];
bool cmp(node a,node b)
{
   if(a.flag!=b.flag)
        return a.flag>b.flag;
   else{
     if(a.flag==0)
      return a.l>b.l;
    else
      return a.r<b.r;
   }
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",s);
            int len=strlen(s);
            int num1=0,num2=0,num=0;
            for(int j=0;j<len;j++){
                if(s[j]=='(')
                    num1++;
                else{
                    if(num1){
                        num++;
                        num1--;
                    }
                    else
                        num2++;
                }
            }
            a[i].l=num1;
            a[i].r=num2;
            a[i].num=num;
            if(a[i].l>=a[i].r)
                a[i].flag=1;  //左大
            else
                a[i].flag=0;
        }
        sort(a,a+n,cmp);
       //  for(int i=0;i<n;i++)
       //  printf("%d %d %d\n",a[i].l,a[i].r,a[i].flag);
            ll ans=0,pre=0;
            for(int i=0;i<n;i++){
                ans+=a[i].num;
                if(pre&&a[i].r){
                    if(pre>a[i].r){
                        ans+=a[i].r;
                        pre-=a[i].r;
                    }
                    else{
                         ans+=pre;
                         pre=0;
                    }
                }
                pre+=a[i].l;
            }
        printf("%lld\n",ans*2);
    }
}

          

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/81225855