hdu 6299 Balanced Sequence (贪心)
Balanced Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6311 Accepted Submission(s): 1648
+ 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.
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.
1
)()(()(
2
)
)(
2
+ if A and B are balanced, AB is balanced,
+ if A is balanced, (A) is balanced.
//优先级排序:
//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 (贪心)的更多相关文章
- HDU 6299 Balanced Sequence(贪心)
题目:给出N个只有左右括号字符串 ,这N个字符串的排列顺序是任意的 , 问按最优的排序后 , 得到最多匹配的括号个数 分析: 我们很容易的想到 字符串)()()(( , 这样的字符串可以精简为)(( ...
- hdu 6299 Balanced Sequence (括号序列,贪心)
大意: 记$f(t)$表示字符串$t$的最长括号匹配子序列, 给定n个括号序列, 求它们重排后的最大f(t). 首先可以注意到一个括号序列中已经匹配的可以直接消去, 一定不会影响最优解. 那么这样最终 ...
- hdu 6299 Balanced Sequence(贪心)题解
题意:题意一开始不是很明白...就是他给你n个串,让你重新排列组合这n个串(每个串内部顺序不变),使得匹配的括号长度最大.注意,题目要求not necessary continuous,括号匹配不需要 ...
- hdu 6299 Balanced Sequence( 2018 Multi-University Training Contest 1 )
#include <stdio.h> #include <iostream> #include <cstdlib> #include <cmath> # ...
- HDU 6299.Balanced Sequence-贪心、前缀和排序 (2018 Multi-University Training Contest 1 1002)
HDU6299.Balanced Sequence 这个题就是将括号处理一下,先把串里能匹配上的先计数去掉,然后统计左半边括号的前缀和以及右半边括号的前缀和,然后结构体排序,然后遍历一遍,贪心策略走一 ...
- hdu6299 Balanced Sequence 贪心
题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...
- hdu 6047 Maximum Sequence 贪心
Description Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: ...
- HDU 6047 Maximum Sequence(贪心+线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- HDU 6047 Maximum Sequence (贪心+单调队列)
题意:给定一个序列,让你构造出一个序列,满足条件,且最大.条件是 选取一个ai <= max{a[b[j], j]-j} 析:贪心,贪心策略就是先尽量产生大的,所以就是对于B序列尽量从头开始,由 ...
随机推荐
- element表格点击行即选中该行复选框
关键代码如下 <el-table ref="multipleTable" :data="tableData" highlight-current-row ...
- python2中的SSL:CERTIFICATE_VERIFY_FAILED错误的解决办法
在使用urllib2访问一个自签名的https链接时,对于python2.6以下版本,TLS握手期间是不会检查服务器X509的证书签名是否是CA的可信任根证书.不过python2.7以后改变了这种情况 ...
- PHP编程20大效率要点
1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row[’id’] 的速度是$row[id]的7倍. 3.echo 比 print 快,并且使用ech ...
- Photoshop CS5软件安装教程
Photoshop CS5(32/64位)精简版下载地址: 链接:https://pan.baidu.com/s/11T-L-aH3JNXlJmSqL0JY6Q提取码:ea5j Photoshop主要 ...
- Leetcode 1020 飞地的数量
地址 https://leetcode-cn.com/problems/number-of-enclaves/ 给出一个二维数组 A,每个单元格为 0(代表海)或 1(代表陆地). 移动是指在陆地上从 ...
- 堆的python实现及其应用
堆的概念 优先队列(priority queue)是一种特殊的队列,取出元素的顺序是按照元素的优先权(关键字)大小,而不是进入队列的顺序,堆就是一种优先队列的实现.堆一般是由数组实现的,逻辑上堆可以被 ...
- CCNA 之 七 路由协议 三 OSPF
OSPF协议 OSPF开放式最短路径优先 全称:Open Shortest Path First 是目前使用最为广泛的路由协议,主要因为OSPF是开放式协议,和IGRP.EIGRP思科的私有协议不同. ...
- centos 7 MysSQL 5.7.23 源码安装
MySQL 5.7.23 源码安装 CentOS 7 将默认数据库MySQL替换成了Mariadb. 这里会从系统的环境准备开始一步一步安装. 环境准备 系统版本 内核版本 IP地址 Centos 7 ...
- python calendar 时间处理类库
#python中的calendar import calendar #返回指定年的某月 def get_month(year, month): return calendar.month(year, ...
- Nginx配置详细解析(全)
一.nginx.conf文件结构 (1)共三部分:由全局块.events块.http块组成.http块又包含http全局块.server块:server块由多个location块组成. (2)一般情况 ...