Balanced Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3780 Accepted Submission(s): 991
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,只有右括号 ))))))))
这样,就可以进入第二步了,1 和 3 这种情况好理解,但是第二种需要注意,我们假设一个贡献值,(贡献值 = 左括号数量 - 右括号数量 ) 这样可以看出这个字符串左括号多还是右括号多,以及多多少,然后按照贪心策略去排序【重点】
贪心策略 :
1,在左括号比较多的情况下,右括号少的放左边,即左括号都做正贡献的时候,右括号少的放左边,
为的是尽量的消除2串中右括号的数量,就可以把左括号归到1里面去,这样,就可以减少浪费
2,左括号做正贡献的放左边
3,左括号做负贡献的话,左括号多的放左边
23都是同理。
贴代码了 ~~~~
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 100010
using namespace std;
int ans;
struct node{
int left,right,l_r; // l_r 表示 left - right ,即差值
}nod[100005];
vector<node> v1,v2,v3;
bool cmp(node a,node b){
if(a.l_r >= 0 && b.l_r >= 0){
return a.right < b.right;
}else if(a.l_r >= 0){
return true;
}else if(b.l_r >= 0){
return false;
}else{
return a.left > b.left;
}
}
void init(){
v1.clear();
v2.clear();
v3.clear();
}
int main (){
int T;
scanf("%d",&T);
while(T--){
init();
ans = 0;
int n;
scanf("%d",&n);
stack<int>sta;
char ch[maxn];
for(int i = 0;i < n;i++){
scanf("%s",ch);
// puts(ch);
int len = strlen(ch);
for(int j = 0;j < len;j++){
if(j == 0 || ch[j] == '('){
sta.push(ch[j]);
}else if(ch[j] == ')'){
if(!sta.empty() && sta.top() == '('){
sta.pop();
ans++;
// cout << "~" << endl;
}else{
sta.push(ch[j]);
}
}
} // sta
nod[i].right = nod[i].left = nod[i].l_r = 0;
while(!sta.empty()){
char tem = sta.top();
if(tem == '('){
nod[i].left++;
}else if(tem == ')'){
nod[i].right++;
}
sta.pop();
}
nod[i].l_r = nod[i].left - nod[i].right;
if(nod[i].left > 0 && nod[i].right == 0){
v1.push_back(nod[i]);
}else if(nod[i].left == 0 && nod[i].right > 0){
v3.push_back(nod[i]);
}else if(nod[i].left > 0 && nod[i].right > 0){
v2.push_back(nod[i]);
}
}
if(v2.size() > 1){
sort(v2.begin(),v2.end(),cmp);
}
int left = 0;
for(int i = 0;i < v1.size();i++){
left += v1[i].left;
} // 从第一种情况提取全部
for(int i = 0;i < v2.size();i++){
int tmp = min(left,v2[i].right); // tmp为可合并数目
ans += tmp; // 答案增加
left = left - tmp + v2[i].left; // 左边左括号减少
}
for(int i = 0;i < v3.size();i++){
int tmp = min(left,v3[i].right); // 右侧合并
ans += tmp;
left -= tmp;
if(left == 0)
break;
}
printf("%d\n",ans * 2);
}
}