hdu6299 Balanced Sequence 贪心

博客围绕Balanced Sequence问题展开,给出n个字符串,需计算组合后最长的平衡字符子序列长度。思路是先将字符串处理成不合法形式,记录左右括号数量,通过比较min(a1,b2)与min(a2,b1)确定字符串排列顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目传送门

题目大意:给出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 = 1; int x = 0; char s = getchar();
    while (s<'0' || s>'9') { if (s == '-')f = -1; s = getchar(); }
    while (s >= '0'&&s <= '9') { x = x * 10 + s - '0'; s = getchar(); }x *= f;
    return x;
}
inline ll gcd(ll a, ll b) {
    if (b == 0)return a;
    return gcd(b, a%b);
}
struct s
{
    int a, b;
}z[100010];
char c[100010];
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 = 0;
        scanf("%d", &n);
        for (i = 0; i < n; i++)
        {
            scanf("%s", c);
            len = strlen(c);
            a = 0;
            b = 0;
            for (j = 0; 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 = 0;
        b = 0;
        for (i = 0; i < n; i++)
        {
            if (a >= z[i].b)
            {
                ans += z[i].b;
                a -= z[i].b;
            }
            else
            {
                ans += a;
                a = 0;
            }
            a += z[i].a;
        }
        printf("%lld\n", ans * 2);
    }
    return 0;
}

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 (1n105) -- 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

转载于:https://www.cnblogs.com/mountaink/p/9558408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值