这个题的题意是给你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. Angular-cli新建项目目录结构详解

    Angular-cli新建项目目录结构详解 在上一篇博客中我们已经通过Angular CLI命令行工具创建出来一个全新的Angular项目,要想写项目,首先我们要先搞清楚项目的目录结构是怎样的,每个文 ...

  2. http://www.jb51.net/list/list_233_2.htm(导航: 首页 >> 软件编程 >> Android)

      日期:2015-04-24理解Android中Activity的方法回调 日期:2015-04-24Android获取手机通讯录.sim卡联系人及调用拨号界面方法 日期:2015-04-24And ...

  3. window 下编译cef 内核 加入mp3/mp4 支持

    下载 depot_tools 解压,加入到环境变量 进入cmd(管理员)运行 gclient 获取 python和git,svn,设置python环境变量 创建新文件夹 mkdir chromium ...

  4. S3C6410移植u-boot

    1.首先下载u-boot(ftp://ftp.denx.de/pub/u-boot) wget ftp://ftp.denx.de/pub/u-boot/u-boot-latest.tar.bz2 2 ...

  5. java用JDBC连接数据库的方式

    //驱动位置String sDBDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//连接数据库地址名字String ...

  6. maven学习总结-eclipse开发

    一.创建Web项目 1.1 选择建立Maven Project 选择File -> New ->Project,如下图所示:

  7. SpringBoot RestController 同时支持返回xml和json格式数据

    @RestController 默认支持返回json格式数据,即使不做任何配置也能返回json数据 当接口需要支持xml或json两种格式数据时应该怎么做呢? 只要引入 Jackson xml的 ma ...

  8. memcache 加载(对象)所遇到的问题。资源

    <?php $mem =new memcache(); if($mem->connect('127.0.0.1','11211')){ echo '连接OK'.'<br>'; ...

  9. Python中sort与sorted函数

    python中列表的内置函数sort()可以对列表中的元素进行排序,而全局性的sorted()函数则对所有可迭代的序列都是适用的: 并且sort()函数是内置函数,会改变当前对象,而sorted()函 ...

  10. 5.WHERE 子句

    WHERE 子句用于规定选择的标准. WHERE 子句 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句. 语法 SELECT 列名称 FROM 表名称 WHERE 列 运 ...