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序列尽量从头开始,由 ...
随机推荐
- ubuntu 16.04上源码编译opengv | compile opengv on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/1e5d14ee/,欢迎阅读! compile opengv on ubuntu 16.04 Series compile open ...
- W5500设计方案
W5500是韩国一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,W5500同时也是一颗工业级以太网控制芯片,最近发现我们国内也有和W5500 芯片一样芯片 介绍给大家 如下图:
- python3 之 面向对象(类)、继承、派生和多态
类提供了一种 组合数据和功能 的方法.创建一个新类意味着:创建一个新 类型 的对象,从而允许创建一个该类型的新 实例. 每个类的实例可以拥有: 保存自己状态的属性. 一个类的实例也可以有改变自己状态 ...
- 2019-9-9:渗透测试,基础学习,windows基础命令,笔记
windows系统基础命令学习 1,命令提示符界面进入方法 方法一: 某分区按住shift,右键单击选择在此处打开windows powershell,进入之后输入cmd 方法二:标题栏输入 方法三: ...
- Session.run() & Tensor.eval()
如果有一个Tensor t,在使用t.eval()时,等价于: tf.get_defaut_session().run(t) t = tf.constant(42.0) sess = tf.Sessi ...
- CentOS 7 Cobbler 安装
Cobbler介绍 Cobbler是一个Linux服务器快速网络安装的服务,而且在经过调整也可以支持网络安装windows. 使用python开发,小巧轻便(才15k行python代码),可以通过网络 ...
- 判断一个坐标点是否在封闭曲线内的方法(swift)
//用playground运行即可 import UIKit var str = "Hello, playground" let lTestPointsOne : [(Double ...
- Internet History,Technology,and Security - The Web Makes it Easy to Use(Week3)
时间如白驹过隙,又到了新的一周的慕课学习啦.这周内容较为简单,主要讲述互联网内部的发展状况. The Early World-Wide-Web Getting to the Web 谈到万维网,我们不 ...
- iOS app反编译
对于APP store 上的应用都是加密的了,反编译起来有难度. 对于自己用xcode 编译的ipa 或者是其他渠道下载的ipa包都可以使用反编译工具进行反编译. https://cnbin.gith ...
- DAO模式多表联查
student类: package com.myschool.entity; public class student{ private int studentno; //学号 private S ...