HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence

本文介绍了一种通过排序和拼接多个由括号组成的字符串来形成最长平衡字符串的方法。平衡字符串定义为每对括号都能正确闭合的字符串。文章详细解释了排序规则,并提供了一个C++实现示例。
这个题的题意是给你n个字符串,认定()是一种平衡的串,两个以上连续的()()也是一种平衡的串,如果一对括号里面包含一个平衡的串,这个括号也被算在这个平衡的串之内,
如(()(()))是一个长度为8的平衡字符串,让你通过对给的n个串进行排序,组成一个新的串,问其中平衡串的最大长度是多少
这个题的重点是在对这些串排序规则的制定(本鶸连要排序都想不到)
如果要得到一个最长平衡串的串
需要遵循以下几点规则
注:左为'(',右为')'
1.左多右少的串在右多左少的左边
2.左少右多和左少右多排序时,左括号少的放在右边
3.其他情况按右边少的放在左边
 
这个题最重要的一点是要知道 左括号可以一直用下去,而右括号在匹配到下一个串时就失效了,所以要尽可能多的利用右括号
左右相等的其实放在左多右少和右多左少里面都可以
 
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 (1n105) -- 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 #include <iostream>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <math.h>
 5 
 6 using namespace std;
 7 
 8 struct node
 9 {
10     int l, r;
11     node() {}
12     node(int a, int b) { l = a, r = b; }
13     bool operator < (const node &a) const
14     {
15         if (r >= l && a.r >= a.l)
16             return l>a.l;
17         else if (l > r && a.l <= a.r)
18             return true;
19         else if (l <= r && a.l > a.r)
20             return false;
21         else
22             return r < a.r;
23     }
24 }node[100010];
25 
26 
27 int main()
28 {
29     int t;
30     scanf("%d", &t);
31 
32     while (t--)
33     {
34         int n;
35         long long num = 0;
36         scanf("%d", &n);
37         char ch[100010];
38         for (int i = 0; i < n; i++)
39         {
40             scanf("%s", ch);
41             int len = strlen(ch);
42             int l = 0, r = 0;
43             for (int j = 0; j < len; j++)
44             {
45                 if (ch[j] == '(')
46                     l++;
47                 else
48                 {
49                     if (l)
50                     {
51                         l--;
52                         num += 2;
53                     }
54                     else
55                         r++;
56                 }
57 
58                 node[i].l = l;
59                 node[i].r = r;
60             }
61         }
62         sort(node, node + n);
63         //for (int i = 0; i < n; i++)
64         //cout << node[i].l << ends << node[i].r << endl;
65         int l = 0;
66         for (int i = 0; i < n; i++)
67         {
68             int r = node[i].r;
69             if (l&&r)
70             {
71                 int x = min(l, r);
72                 num += x * 2;
73                 l -= x;
74             }
75             l += node[i].l;
76         }
77         cout << num << endl;
78     }
79     return 0;
80 }

 

转载于:https://www.cnblogs.com/qq965921539/p/9364181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值