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
  • 相关阅读:
    SharePoint 2010 + Infopath 2010 VSTA + WF + Timer Job + BI 之 报销单实例系列创建报销单新增视图
    读取 SPFieldChoice 选项类型的三种方法 ,第三种方法目前有问题没有解决,请高手来解决一下
    Ubuntu SVN安装
    Hibernate注解使用以及Spring整合
    性能大提速 MyEclipse终极优化四要点
    MyEclipse运行错误解决办法:The Linux WYSIWYG design panel is still under development
    详细介绍Ubuntu VirtualBox的桥接网络配置
    Log4Net使用指南
    MyEclipse Java Build Path详解
    JSTL 核心标签库 使用
  • 原文地址:https://www.cnblogs.com/mountaink/p/9558408.html
Copyright © 2011-2022 走看看