zoukankan      html  css  js  c++  java
  • 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


    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
     
    题目大意:
    给你n个由'('和')'组成的括号序列。要你将这n个序列整体排序使得匹配的子序列(不一定相连)最长。求最长子序列。匹配条件如下:
    + if it is the empty string
    + if A and B are balanced, AB is balanced,
    + if A is balanced, (A) is balanced.
     
    贪心。
    首先将每个序列中匹配的括号计数并剔除。可以看到,剩下的序列都行如")))(((((",用结构体存储r,l,排序,就可以贪心了。
    贪心方法是(其实我并不能证明,大概是基于左括号尽量往左放,vice versa的思想):
    1、"))))((((" 中 ')' < '(' 的 , 按 ')' 从小到大排序 ;
    2、"))))((((" 中 ')' >= '(' 的 , 按 '(' 从大到小排序 ;
     
    //优先级排序:
    //1、"))))((((" 中 ')' < '(' 的 , 按 ')' 从小到大排序 ;
    //2、"))))((((" 中 ')' >= '(' 的 , 按 '(' 从大到小排序 ;
    
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<stack>
    
    using namespace std;
    
    const int maxn=100000;
    
    char s[maxn+10];
    
    struct tstr
    {
        int r,l;
    };
    tstr str[maxn+10];
    
    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=0;
            for(int i=0;i<n;i++)
            {
                scanf("%s",s);
                stack<char> sta;
                sta.push(')');//在栈底放一个')',方便后续操作
                for(int j=0;s[j]!='';j++)
                {
                    if(s[j]==')'&&sta.top()=='(')
                        ans+=2,sta.pop();
                    else
                        sta.push(s[j]);
                }
                str[i].r=-1;str[i].l=0;
                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=0;i<n;i++)
            {
                for(int j=0;j<str[i].r;j++)
                {
                    if(sta.top()=='(')
                        ans+=2,sta.pop();
                    else
                        sta.push(')');
                }
                for(int j=0;j<str[i].l;j++)
                {
                    sta.push('(');
                }
            }
    
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    在小程序开发的新风口 看华为云如何助立创科技抢占市场红利
    华为云软件开发云助力集时通软件敏捷开发
    一站式云端安卓软件开发工具的体验之路!
    一名优秀的全栈工程师必需的开发工具
    学哪种编程语言更有“钱”赚?
    深度剖析:最新云端开发工具如何实现敏捷+DevOps开发落地
    软件开发未来发展五大趋势,从业者们注意了!
    十个最有“钱景”的IT技能, 你掌握了哪个?
    “敏捷开发”之白话篇
    解决软件开发中的多个痛点——华为软件开发云
  • 原文地址:https://www.cnblogs.com/acboyty/p/9684014.html
Copyright © 2011-2022 走看看