2018多校第一场 1002 Balanced Sequence 【补题】 贪心法

1 篇文章 0 订阅

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);	
		
	}
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值