hdu 多校联赛 Balanced Sequence

                  Balanced Sequence

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


 

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   

/*left代表左括号, right代表右括号;
根据题的意思,一个串的内部不能排序,出现这样()才是此题的答案,需乘以二;
当出现多个串的时候,会有几种情况;
(1) )))))(( 右括号大于左括号
(2) ))((((((右括号小于左括号
(3) ))))))(( 右括号大于左括号
(4) )))))))((((右括号大于左括号
(5) )))((( 右括号等于于左括号
若是符合(1),(2)的情况,当(1)放在(2)的后面才能得到更多的这样();
若是符合(3),(2)的情况,当(3)放在(2)的后面才能得到更多的这样();
*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<stack>
#include<string.h>
const int maxn=1e5+5;
using namespace std;
char ss[maxn];
struct std1
{
    int left,right;
}a[maxn];
int cmp(std1 m1,std1 m2)
{
    if(m1.left<=m1.right&&m2.left>m2.right)
        return 0;
    if(m1.left>m1.right&&m2.left<=m2.right)
        return 1;
    if(m1.left<=m1.right&&m2.left<=m2.right)
        return m1.left>m2.left;
    return m1.right<m2.right;
}
int main()
{
    int t,n;
    long long int ans;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            stack<char> s1;
            while(!s1.empty())//栈的初始化;
                s1.pop();
            scanf("%s",ss);
            int len=strlen(ss);//由于计算字符串的长度,最多需要100000,而for循环也是100000,
            for(int j=0; j<len; j++) //若是把for循环里的len换成strlen(ss),则会炸掉
            {                       //若为(则进栈,若为),则看是否栈为空,存在栈顶元素为(,
                if(ss[j]=='(')
                {                    //  若满足此条件,则让(出栈,而此时)并未进栈,继续下一个字符判断;
                    s1.push(ss[j]);//若为),且栈顶元素不为(,则进栈;此时的栈中不存在这样();
                }
                else
                {
                    if(!s1.empty()&&s1.top()=='(')
                    {
                        ans++;
                        s1.pop();
                    }
                    else
                    {
                        s1.push(ss[j]);
                    }
                }
            }
            int left=0,right=0;
            while(!s1.empty())
            {
                if(s1.top()=='(')//计算左括号和右括号的个数
                {
                    left++;
                }
                else
                {
                    right++;
                }
                s1.pop();
            }
            a[i].left=left;
            a[i].right=right;
        }
        sort(a+1,a+n+1,cmp);
        int sy=0;
        for(int i=1; i<=n; i++)
        {
            if(a[i].right>sy) //若是以右括号为主,则需要让右括号标记为0,sy标记为剩余的括号,
            {                   //所以,sy需要减去右括号的数量,加上左括号的数量
                a[i].right=sy;
            }
            ans=ans+a[i].right;
            sy=sy-a[i].right;
            sy=sy+a[i].left;
        }
        printf("%lld\n",ans*2);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/smilelingling/article/details/81347099