zoukankan      html  css  js  c++  java
  • hdu6299 Balanced Sequence 贪心

    题目传送门

    题目大意:给出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
    ", 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
  • 相关阅读:
    docker compose 配置 redis cluster jenkins
    Spring Core
    Java Case Interview two
    pytest 生成 allure报告(含4要素的对应版本,兼容)
    python中requests库的post请求 4种类型参数
    接口测试流程
    Docker学习篇 搭建jenkins
    Pytest入门 实例
    python selenium css定位6种
    python selenium select标签的下拉框和非select标签的下拉框
  • 原文地址:https://www.cnblogs.com/mountaink/p/9558408.html
Copyright © 2011-2022 走看看