hdu 6299 Balanced Sequence

Balanced Sequence

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


 

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

思路:

每个字符串先内部处理,能相互抵消的先抵消。
然后便形成了形如 1.)))((  2.))((((((  3.)))))( 4.(((
然后按照一定顺序对n个字符串排序,
记最后剩下的左括号的数量为l,右括号的数量为r,
r==0 的在前,之后是l>=r的(r小的在先),
然后是l
#include
#include
using namespace std;
const int maxn=1e5+10;
struct node
{
    int l,r;
}c[maxn];
char t[maxn];
bool cmp(const node &a,const node &b)
{
    if(a.r==0) return 1;
    if(b.r==0) return 0;
    if(a.l>=a.r&&b.l=b.r) return 0;
    if(a.l>=a.r&&b.l>=b.r) return a.r<=b.r;
    return a.l>=b.l;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        int ans=0;
        int n;scanf("%d",&n);
        for(int i=0;i

 

你可能感兴趣的:(hdu 6299 Balanced Sequence)