题目传送门

题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度。

思路:

首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b,考虑两个字符串,这两个如果min(a1,b2)小于min(a2,b1)时,第一个字符串是不是应该排在前面呢?如果两个相同的话,当然应该吧右括号比较多的放在前面,左括号比较多的放在后面。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
inline int rd() {
int f = ; int x = ; char s = getchar();
while (s<'' || s>'') { if (s == '-')f = -; s = getchar(); }
while (s >= ''&&s <= '') { x = x * + s - ''; s = getchar(); }x *= f;
return x;
}
inline ll gcd(ll a, ll b) {
if (b == )return a;
return gcd(b, a%b);
}
struct s
{
int a, b;
}z[];
char c[];
bool comp(s a, s b)
{
int x = min(a.a, b.b);
int y = min(a.b, b.a);
if (x == y)
{
if (a.a == b.a)
{
return a.b < b.b;
}
return a.a > b.a;
}
return x > y;
}
int main(void)
{
int t, n, i, len, j, a, b;
long long ans;
scanf("%d", &t);
while (t--)
{
ans = ;
scanf("%d", &n);
for (i = ; i < n; i++)
{
scanf("%s", c);
len = strlen(c);
a = ;
b = ;
for (j = ; j < len; j++)
{
if (c[j] == '(')
{
a++;
}
else
{
if (a)
{
a--;
ans++;
}
else
{
b++;
}
}
}
z[i].a = a;
z[i].b = b;
}
sort(z, z + n, comp);
a = ;
b = ;
for (i = ; i < n; i++)
{
if (a >= z[i].b)
{
ans += z[i].b;
a -= z[i].b;
}
else
{
ans += a;
a = ;
}
a += z[i].a;
}
printf("%lld\n", ans * );
}
return ;
}

Balanced Sequence

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

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

hdu6299 Balanced Sequence 贪心的更多相关文章

  1. HDU6299 Balanced Sequence (多校第一场1002) (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. HDU 6299 Balanced Sequence(贪心)

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

  3. hdu 6299 Balanced Sequence (贪心)

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. hdu多校1002 Balanced Sequence

    Balanced Sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...

  5. HDU5014Number Sequence(贪心)

    HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...

  6. HDU 多校对抗赛 B Balanced Sequence

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

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

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

  9. Balanced Sequence(毒瘤啊)排序贪心 HDU多校

    Problem Description Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this type ...

随机推荐

  1. git pull没有指定branch的报错

    执行git pull或者git push的时,有时候会出现如下报错: $ git pull You asked me to pull without telling me which branch y ...

  2. java的邮件系统实现

    想要java中邮件发送和接收邮件,首先需要支持SMTP- pop/pop3/IMAP协议,发送的话还需要配置文件,来对程序提供相应的接口,只需要这两个文件,就可以实现邮件的接收发送, 协议为jar包封 ...

  3. 【转】PEAR安装、管理及使用

    PEAR安装   linux下只要你安装的是PHP 4.3.0以上的版本,默认安装都是支持PEAR的,除非你使用了”--WITHOUT-PEAR”选项,修改PHP.INI文件,在INCLUDE_PAT ...

  4. ROS Learning-019 learning_tf-03(编程) 添加额外的坐标系 (Python版)

    ROS Indigo learning_tf-03 添加额外的坐标系 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.0 ...

  5. 一道java笔试题目:Vector和ArrayList的区别

    Vector和ArrayList的区别 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构这些类均在java.util包中本文试图通过 ...

  6. boost.asio系列(一)——deadline_timer

    一.构造函数 一个deadline_timer只维护一个超时时间,一个deadline_timer不同时维护多个定时器.在构造deadline_timer时指定时间: basic_deadline_t ...

  7. Bugly升级应用集成指南

    1.配置 app/build.gradle android { defaultConfig { ndk { //设置支持的SO库架构 abiFilters 'armeabi' //, 'x86', ' ...

  8. python 爬虫之requests+日志+配置文件读取+mysql入库

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 日志管理 import logging import sys reload(sys) sys.setde ...

  9. 【C#】EF学习<二> DbFirst (先创建数据库,表及其关联关系)

    工程压缩文件放到百度云盘---20181019001文件夹 1. 创建表的脚本 create table Teacher ( TID char(12) primary key, Tname char( ...

  10. UWP&WP8.1 基础控件—Button

    Button作为最常用的控件,没有特别难的用法,是一个非常简单,可以很快就掌握的控件. Button 基础用法: 同样,在UWP项目中,可以从工具箱中拖拽到面板中进行使用.也可以使用XAML语法进行编 ...