hdu 6299


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.

 


大致提议:

给你n个窜,问你怎么排序才输出的()个数最多,输出最多那个的个数。

比如第一个样例:)   ()    (   ()   (  能组两个()。所以答案是2 * 2 为4。

第二个样例  最多能组成   )()。答案是 1 * 2 为2。

思路: 先用栈来实现去掉每一个子串里面已经成()的括号,最后剩下的就只有三种情况:

1;(((((((((

2 :)))))))))

3   :))))))(((((((

那么现在将这三种情况的残留子串做个重新排序的话,就能创造更多的合法子串.

第一种情况肯定放最左边

第二最右

第三种情况引入一个概念:左括号的贡献(左括号减去右括号的值)大的放右边,小的放左。

代码实现:用栈实现去掉每一个子串里面已经成()的括号。。定义3个vector存残留子串,结构体node来存左右括号剩余的个数,和左括号的贡献

tmp辅助排序.

不说了,贴代码

#include
using namespace std;
stack  st;
char inn[100005];
struct node{
	int l,r,ans;
}nod[100005];
bool tmp(node a,node b){
	if(a.ans > 0 && b.ans > 0){
		return a.r < b.r;
	}else if(a.ans > 0){
	    return 1;
	}else if(b.ans > 0){
		return 0;
	}else {
		return a.l > b.l;
	}
}
vector v1,v2,v3;
void init (){
	v1.clear();
	v2.clear();
	v3.clear();
}
int leng,ans = 0,tmpp;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int a;
		int ans = 0;
		init();
		scanf("%d",&a);
		getchar();
		for(int i = 0;i < a;i++){
			gets(inn);
			leng = strlen(inn);
			for(int j = 0;j < leng;j++){
				if(j == 0){
					st.push(inn[j]);
				}else if(inn[j] == '('){
					st.push(inn[j]);
				}else{
					if(!st.empty() && st.top() == '('){
						ans++;
						st.pop();
					}else{
						st.push(inn[j]);
					}
				}
			}
			nod[i].l = 0;
			nod[i].r = 0;
			while(!st.empty()){
				if(st.top() == '('){
					nod[i].l ++;
				}else{
					nod[i].r ++;
				}
				st.pop();
			}
			nod[i].ans = nod[i].l - nod[i].r;
			if(nod[i].l > 0 && nod[i].r == 0){
				v1.push_back(nod[i]);
			}else if(nod[i].l == 0 && nod[i].r > 0){
				v3.push_back(nod[i]);
			}else{
				v2.push_back(nod[i]);
			}
		}
		if(v2.size() > 1){
			sort(v2.begin(),v2.end(),tmp);
		}
		int left = 0;
		for(int i = 0;i < v1.size();i++){
			left += v1[i].l;
		}
		for(int i = 0;i < v2.size();i++){
			if(v2[i].r < left){
				tmpp = v2[i].r;
			}else{
				tmpp = left;
			}
			left = left - tmpp + v2[i].l;
			ans += tmpp;
		}
		for(int i = 0;i < v3.size();i++){
			if(v3[i].r < left){
				tmpp = v3[i].r;
			}else{
				tmpp = left;
			}
			ans += tmpp;
			left -= tmpp;
			if(left == 0){
				break;
			}
		}
		printf("%d\n",ans * 2);
	}
	return 0;
}

 

 

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