Balanced Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6311    Accepted Submission(s): 1648

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
 
题目大意:
给你n个由'('和')'组成的括号序列。要你将这n个序列整体排序使得匹配的子序列(不一定相连)最长。求最长子序列。匹配条件如下:
+ if it is the empty string
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced.
 
贪心。
首先将每个序列中匹配的括号计数并剔除。可以看到,剩下的序列都行如")))(((((",用结构体存储r,l,排序,就可以贪心了。
贪心方法是(其实我并不能证明,大概是基于左括号尽量往左放,vice versa的思想):
1、"))))((((" 中 ')' < '(' 的 , 按 ')' 从小到大排序 ;
2、"))))((((" 中 ')' >= '(' 的 , 按 '(' 从大到小排序 ;
 
//优先级排序:
//1、"))))((((" 中 ')' < '(' 的 , 按 ')' 从小到大排序 ;
//2、"))))((((" 中 ')' >= '(' 的 , 按 '(' 从大到小排序 ; #include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack> using namespace std; const int maxn=; char s[maxn+]; struct tstr
{
int r,l;
};
tstr str[maxn+]; bool cmp(tstr a,tstr b)
{
if(a.r<a.l&&b.r>=b.l)
return true;
if(b.r<b.l&&a.r>=a.l)
return false;
if(a.r<a.l&&b.r<b.l)
return a.r<b.r;
else
return a.l>b.l;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n); int ans=;
for(int i=;i<n;i++)
{
scanf("%s",s);
stack<char> sta;
sta.push(')');//在栈底放一个')',方便后续操作
for(int j=;s[j]!='\0';j++)
{
if(s[j]==')'&&sta.top()=='(')
ans+=,sta.pop();
else
sta.push(s[j]);
}
str[i].r=-;str[i].l=;
while(!sta.empty())
{
if(sta.top()=='(')
str[i].l++,sta.pop();
if(sta.top()==')')
str[i].r++,sta.pop();
}
} sort(str,str+n,cmp);
stack<char> sta;
sta.push(')');
for(int i=;i<n;i++)
{
for(int j=;j<str[i].r;j++)
{
if(sta.top()=='(')
ans+=,sta.pop();
else
sta.push(')');
}
for(int j=;j<str[i].l;j++)
{
sta.push('(');
}
} printf("%d\n",ans);
}
return ;
}

hdu 6299 Balanced Sequence (贪心)的更多相关文章

  1. HDU 6299 Balanced Sequence(贪心)

    题目:给出N个只有左右括号字符串 ,这N个字符串的排列顺序是任意的 , 问按最优的排序后 , 得到最多匹配的括号个数 分析: 我们很容易的想到 字符串)()()(( , 这样的字符串可以精简为)(( ...

  2. hdu 6299 Balanced Sequence (括号序列,贪心)

    大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t). 首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解. 那么这样最终 ...

  3. hdu 6299 Balanced Sequence(贪心)题解

    题意:题意一开始不是很明白...就是他给你n个串,让你重新排列组合这n个串(每个串内部顺序不变),使得匹配的括号长度最大.注意,题目要求not necessary continuous,括号匹配不需要 ...

  4. hdu 6299 Balanced Sequence( 2018 Multi-University Training Contest 1 )

    #include <stdio.h> #include <iostream> #include <cstdlib> #include <cmath> # ...

  5. HDU 6299.Balanced Sequence-贪心、前缀和排序 (2018 Multi-University Training Contest 1 1002)

    HDU6299.Balanced Sequence 这个题就是将括号处理一下,先把串里能匹配上的先计数去掉,然后统计左半边括号的前缀和以及右半边括号的前缀和,然后结构体排序,然后遍历一遍,贪心策略走一 ...

  6. hdu6299 Balanced Sequence 贪心

    题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...

  7. hdu 6047 Maximum Sequence 贪心

    Description Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: ...

  8. HDU 6047 Maximum Sequence(贪心+线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  9. HDU 6047 Maximum Sequence (贪心+单调队列)

    题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由 ...

随机推荐

  1. LeetCode 5272. 5272. 统计参与通信的服务器 Count Servers that Communicate

    地址 https://leetcode-cn.com/problems/count-servers-that-communicate/ 题目描述这里有一幅服务器分布图,服务器的位置标识在 m * n  ...

  2. javescript 的 对象

    一,定义:对象是JavaScript的一个基本数据类型,是一种复合值,它将很多值(原始值或者其他对象)聚合在一起,可通过名字(name/作为属性名)访问这些值.即属性的无序集合. 关键是name属性名 ...

  3. 官网例子,mt-field password获取不到

    新尝试了Mint-UI,在使用表单组件Field时, 直接从demo中拷贝了如下代码: <mt-field label="username" placeholder=&quo ...

  4. [Ubuntu篇] 在ubuntu上源码编译gtest,编写gtest-config.cmake并测试

    本文首发于个人博客https://kezunlin.me/post/4a1427cf/,欢迎阅读! compile gtest on ubuntu 16.04 Guide compile gtest ...

  5. wordpress 获取指定作者或者文章的所有评论数量

    wordpress 获取指定作者或者文章的所有评论数量 <?php $args = array( 'post_author' => '' // fill in post author ID ...

  6. Linux目录结构-上部

    第1章 目录结构 1.1 目录结构特点 倒挂的树状结构一切从根开始一切皆文件 1.2 目录结构 /bin            二进制文件  命令 /sbin           超级命令只有root ...

  7. 阿里P7整理“硬核”面试文档:Java基础+数据库+算法+框架技术等

    现在的程序员越来越多,大部分的程序员都想着自己能够进入大厂工作,但每个人的能力都是有差距的,所以并不是人人都能跨进BATJ.即使如此,但身在职场的我们一刻也不能懈怠,既然对BATJ好奇,那么就要朝这个 ...

  8. Zookeeper 应用实现-配置中心

    一.目标 一个乞丐版自更新配置中心,更新配置后,能在各个服务器实现更新 二.架构 三.角色 config-web: 配置后台,主要用于管理配置,增改配置 config-agent: 监听配置,遇到变动 ...

  9. mac 终端高亮显示~

    针对terminal采用bash模式: 编辑 ~/.bash_profile, 加入以下代码: export CLICOLOR=1 export LSCOLORS=gxfxaxdxcxegedabag ...

  10. Chapter 05—Advanced data management(Part 2)

    二. 控制流 statement:一个单独的R语句或者是一个复合的R语句: cond:条件表达式,为TRUE或FALSE: expr:数字或字符表达式: seq:数字或字符串的顺序. 1.循环语句:f ...