1. 程式人生 > >HDU 6299 Balanced Sequence 括號匹配 貪心

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);
    }
}