这个题的题意是给你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 (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
 #include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h> using namespace std; struct node
{
int l, r;
node() {}
node(int a, int b) { l = a, r = b; }
bool operator < (const node &a) const
{
if (r >= l && a.r >= a.l)
return l>a.l;
else if (l > r && a.l <= a.r)
return true;
else if (l <= r && a.l > a.r)
return false;
else
return r < a.r;
}
}node[]; int main()
{
int t;
scanf("%d", &t); while (t--)
{
int n;
long long num = ;
scanf("%d", &n);
char ch[];
for (int i = ; i < n; i++)
{
scanf("%s", ch);
int len = strlen(ch);
int l = , r = ;
for (int j = ; j < len; j++)
{
if (ch[j] == '(')
l++;
else
{
if (l)
{
l--;
num += ;
}
else
r++;
} node[i].l = l;
node[i].r = r;
}
}
sort(node, node + n);
//for (int i = 0; i < n; i++)
//cout << node[i].l << ends << node[i].r << endl;
int l = ;
for (int i = ; i < n; i++)
{
int r = node[i].r;
if (l&&r)
{
int x = min(l, r);
num += x * ;
l -= x;
}
l += node[i].l;
}
cout << num << endl;
}
return ;
}

HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence的更多相关文章

  1. HDU6333-2018ACM暑假多校联合训练1002-Harvest of Apples-莫队+费马小定理

    题意很简单啦,求S(n,m)的值 通过打表我们可以知道 S(n + 1, m) = S(n, m) * 2 - C(n, m); S(n - 1, m) = (S(n, m) + C(n - 1, m ...

  2. HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造

    Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  3. HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和

    题意是给了一种矩阵的生成方式 让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值) 没看标程之前硬撸写了160行 用了前缀和以后代码量缩短到原来的1/3 根据规律可以推导出这个矩阵是在不断重复 ...

  4. HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

  5. HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube

    就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...

  6. HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组

    本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y) 实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大, ...

  7. HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple

    题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...

  8. HDU6301-2018ACM暑假多校联合训练1004-Distinct Values

    题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...

  9. HDU6308-2018ACM暑假多校联合训练1011-Time Zone

    题目大意就是给你UTC-8时区的时间 让你求对应时区的时间 哇 这个题 看似简单,但是一开始怎么都过不了啊 同学用自己写的read过了,后来看了一下各位大佬说改成分钟随便过,就随便过了 Problem ...

随机推荐

  1. Android 学习 - Telnet 控制AVD

    启动Android Virtual Device之后,使用telnet,可以方便地控制AVD. 首先,获取端口号.启动AVD后,在标题栏上方会出现端口号:设备名,在下面的截图中为5554:N4-15- ...

  2. 判断修改的中的值,用前面的,否则容易获得空值;this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue; VS bool b = (bool)this.dataGridView1.Rows[i].Cells[0].Value;

    判断修改的中的值,用前面的,否则容易获得空值:this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue;  VS     bool b = (b ...

  3. 将.sql文件导入powerdesigner的实现方法详解

    将.sql文件导入powerdesigner的步骤是本文我们主要要介绍的内容,步骤如下: 第一步:将要导入的库的所有表的表结构(不要表数据,只要表结构)导出成一个.sql文件. 第二步:在powerd ...

  4. oracle xe远程访问

    oracle xe其实监听了1521端口 netstat -ano|findstr 只是没请求防火墙权限而已. 手动打开防火墙1521端口 管理员运行下面的命令 本机环境win10 netsh adv ...

  5. apk安装包信息

    String archiveFilePath="sdcard/DangDang.apk";//安装包路径          PackageManager pm = getPacka ...

  6. android解析xml文件的方式

    android解析xml文件的方式   作者:东子哥 ,发布于2012-11-26,来源:博客园   在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xm ...

  7. Perl基础语法

    一.脚本文件perl 代码可以写在一个文本文件中,以 .pl..PL 作为后缀.文件名可以包含数字,符号和字母,但不能包含空格,可以使用下划线(_)来替代空格.一个简单的Perl 文件名:rurun_ ...

  8. Animation Parameter

    [Animation Parameter] Animation Parameters are variables that are defined within the animation syste ...

  9. C++——STL之vector, list, deque容器对比与常用函数

    STL 三种顺序容器的特性对比: vector 可变数组,内存空间是连续的,容量不会进行缩减.支持高效随机存取,即支持[]和at()操作.尾部插入删除效率高,其他位置插删效率较低: list 双向链表 ...

  10. 启动dhcp出错:No subnet declaration for eth0 (192.168.0.1

    XUbuntu 8.04 i386.装了dhcp3-server.使用 sudo /etc/init.d/dhcp3-server start 出错:Apr 30 14:24:03 s dhcpd: ...