HDU 6299 Balanced Sequence (字符串贪心)*

Balanced Sequence

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


 

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

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6331 6330 6329 6328 6327 

#include<bits/stdc++.h>
#define maxn 1000010
using namespace std;
int ans,len,cnt;
int ll,a,b,m;

struct  node
{
    int l,r;///左括号和右括号的数量
};
node s[maxn];
/*
题目大意:给定数个括号字符串,问如何拼接使得出现的()最多。

首先把原来的字符串预先处理完后,发现只剩下三种情况,
全')',全'(',和两种拼接而成的。
在排序时可以考虑,把其中数量多的当成单个括号,
比如如果'('比')'多,不妨把整个序列考虑优先级时考虑成'('的优先级。
当一样的时候手动模拟一下找找规律即可。
*/
bool cmp(node a,node b)
{
    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.r>b.r;
    if(a.l<=b.r&&b.l<=b.r) return a.l<b.l;
}

int main()
{
    int t;scanf("%d",&t);

    char seq[maxn],sk[maxn];
    int top;

    while(t--)
    {
        scanf("%d",&m); ans=0;
        top=0;
        for(int i=0;i<m;i++)
        {
            scanf("%s",seq);
/*
            cnt=0 , len=strlen(seq);///cnt是匹配括号的数量
            ll=0 , a=0 , b=0 ;///ll用于配合计算cnt
*/
            len=strlen(seq);
            for(int j=0;j<len;j++)
            {
                if(seq[j]==')'&&sk[top]=='('&&top>0)
                {
                    ans++;
                    top--;
                }
                else  sk[++top]=seq[j];
            }
            s[i].l=s[i].r=0;
            for(int j=1;j<=top;j++)
            {
                if(sk[j]=='(') s[i].r++;
                else s[i].l++;
            }
        }
        sort(s,s+m,cmp);
        int num=0;
        for(int i=1;i<m;i++)
        {
            num+=s[i-1].r;
            if(s[i].l>=num)
            {
                ans+=num;
                num=0;
            }
            else
            {
                num-=s[i].l;
                ans+=s[i].l;
            }
        }
        printf("%d\n",ans*2);
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81323551