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. Spring 5 响应式编程

    要点 Reactor 是一个运行在 Java8 之上的响应式流框架,它提供了一组响应式风格的 API 除了个别 API 上的区别,它的原理跟 RxJava 很相似 它是第四代响应式框架,支持操作融合, ...

  2. GDG Xi'an DevFest 2019 闪电演讲 -《假如我是一个浏览器》PPT(经典多图,建议收藏)

    GDG Xi'an DevFest2019演讲PPT链接: http://tmp.link/f/5dd9e6bf461b6 闪电演讲<假如我是一个浏览器>PPT链接: https://gi ...

  3. 【论文阅读】Where Is My Mirror?

    Where Is My Mirror?(ICCV2019收录) 作者: 论文链接: https://arxiv.org/pdf/1908.09101.pdf 1.  研究背景 目前存在的计算机视觉任务 ...

  4. sku二维数组里的数组从头到尾叠加组合

    今天工作之余与同事聊天,要是实现一个sku描述里的字段组合的问题.并且实现了请吃饭.哈哈.一顿饭,我和另一位同事积极杠杆的.后来实现了出来. let skuList = [ ['黑色', '白色',' ...

  5. [折腾笔记] 洛谷P1149-火柴棒等式 AC记

    原题链接: https://www.luogu.org/problem/P1149 题面简述: 给你n根火柴棍,你可以拼出多少个形如"A+B=C""A+B=C" ...

  6. mysql如何设置主从(读写分离),redis发布功能,以及redis的持久化存储(rdb,aof)

    1 mysql基本命令 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroot -p -h 127.0.0.1 3.远程链接mysq ...

  7. Java NIO 三大组件之 Channel

    Java NIO 之 Channel 一.什么是Channel Channel用于源节点(例如磁盘)与目的节点的连接,它可以进行读取,写入,映射和读/写文件等操作. 在Java NIO中负责缓冲区中数 ...

  8. .Net Core 3.0 使用 Serilog 把日志记录到 SqlServer

    Serilog简介 Serilog是.net中的诊断日志库,可以在所有的.net平台上面运行.Serilog支持结构化日志记录,对复杂.分布式.异步应用程序的支持非常出色.Serilog可以通过插件的 ...

  9. ERROR: Unrecognized command line argument: 'use'

    Unrecognized command line argument: 'use' gvm--GoLang语言多版本管理工具 基础环境 centos6.5 报错内容 gvm在命令行以外的任何地方调用 ...

  10. Git实战指南----跟着haibiscuit学Git(第五篇)

    笔名:  haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...