2018hdu杭电多校第一场 hdu6299 Balanced Sequence(贪心)

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


题意:  输入T组案例,每组开头输入n个括号字符串,让你将这些括号字符串组合使得组合成的合法匹配的括号子串长度最多。

题解:什么是合法匹配的括号字串,就是类似如下的这几种:()()() (((((())))))  ()()(((())))

那要如何匹配才能使得匹配的括号最多呢?这里就是一个贪心的思路。在前面的要 左括号尽可能左括号多右括号少(因为已经没有左括号和这些右括号进行匹配),同理在后面的要尽可能使得左边的右括号比右边的左括号多。

我们先用栈来对每个括号字符串进行处理(代码种用力对左括号、右括号、和匹配括号数记数的办法),把匹配的括号去掉。最后出现不匹配的括号字符串只有这种 左边全是右括号右边全是左括号的情况: )))))))))((((((  {其中((((、)))))也算这种情况} 

然后对处理后的字符串进行排序,有3种类型的比较,比较出四种情况,详细在代码的cmp函数中。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <set>
#include <deque>
#include <map>
#include <queue>
#include <cassert>
#define INF 0x3f3f3f3f
#define mem(a, x) memset(a, x, sizeof(a))
#define X first
#define Y second
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const double PI = acos(-1.0);
const ll mod=1000000007;
ll powmod(ll a,ll b) {
	ll res=1;
	a%=mod;
	assert(b>=0);
	for(; b; b>>=1) {
		if(b&1)res=res*a%mod;
		a=a*a%mod;
	}
	return res;
}
ll gcd(ll a,ll b) {
	return b?gcd(b,a%b):a;
}
//------------------------------------head------------------------------------
const int N = 1e5+10;
char s[N];
int t, n;

struct node {
	int l, r;
} a[N];

bool cmp(node &a, node &b) {
	if (a.l <= a.r && b.l > b.r) //左括号多右括号少 在 右括号多左括号少 的前面
		return 1;
	else if (a.l > a.r && b.l <= b.r) // 左括号少右括号多 在 右括号少左括号多 的前面
		return 0;
	else if (a.r >= a.l && b.r >= b.l) // 都是左括号少右括号多
		return a.l < b.l; // 左括号少的在前面(右括号多的在后面)
	else // 都是左括号少右括号多
		return a.r > b.r; // 右括号多的在前面
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		int ans = 0;
		rep (i, 0, n) {
			scanf("%s", s);
			int len = strlen(s), cnt_l = 0, cnt_r = 0; // 字符串长度,左括号记数,右括号记数
			rep (j, 0, len) {
				if (s[j] == '(') 
					cnt_l++;
				else if (s[j] == ')' && cnt_l > 0) { // 括号匹配了
					ans++;
					cnt_l--;
				} else
					cnt_r++;
			}
			a[i].l = cnt_l;
			a[i].r = cnt_r;
		}
		sort(a, a+n, cmp);
		int num = 0; // 表示累计的右括号
		rep (i, 1, n) {
			num += a[i-1].r; // 累计右括号
			if (a[i].l > num) { // 左括号能把累计的右括号都匹配掉
				ans += num;
				num = 0;
			} else {
				ans += a[i].l;
				num -= a[i].l;
			}
		}
		printf("%d\n", ans * 2);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40731186/article/details/81197630