这个题的题意是给你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. C#通用模块专题

    开源 程序设计 常用组件 加载图片,播放音乐.视频,摄像头拍照 文件读写(txt.xml.自定义文件格式(后缀名)) 串口通信 稳定的串口读写:http://blog.csdn.net/kolvin2 ...

  2. leetcode892

    这道题因为有0的情况,因此不能使用投影的方法,需要遍历每一个元素,单独处理. class Solution { public: int surfaceArea(vector<vector< ...

  3. CSS JQuyer 元素选择

    $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class="intro" ...

  4. libevent源码深度剖析十二

    libevent源码深度剖析十二 ——让libevent支持多线程 张亮 Libevent本身不是多线程安全的,在多核的时代,如何能充分利用CPU的能力呢,这一节来说说如何在多线程环境中使用libev ...

  5. php格式化时间戳显示友好的时间

    在项目中时间一律显示为2014-10-20 10:22显得很呆板.在微博.QQ空间等网站通常会显示为几秒前,几分钟前,几小时前等容易阅读的时间,我们称之为友好的时间格式.那么用php怎么实现呢? 大体 ...

  6. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  7. 443. String Compression字符串压缩

    [抄题]: Given an array of characters, compress it in-place. The length after compression must always b ...

  8. CMake 自定义编译选项

    自定义编译选项 CMake 允许为项目增加编译选项,从而可以根据用户的环境和需求选择最合适的编译方案. 例如,可以将 MathFunctions 库设为一个可选库,如果该选项为 ON ,就使用该库定义 ...

  9. js加载页面使用execute_script选定加载位置

    #由于js逐步加载页面,存在未显示的网页无法加载源码 from selenium import webdriver driver = webdriver.Firefox() init_element ...

  10. Media Queries 媒体类型

    引用方法:1.<link rel="stylesheet" type="text/css" href="style.css" medi ...