zoukankan      html  css  js  c++  java
  • HDU-6299 Balanced Sequence(2018-HDU多校-第一场-02)

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

    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

    Source

    2018 Multi-University Training Contest 1

    题解:

    首先我们对所有的字符串通过括号匹配找到串内正规括号子序列,之后剩下的串只有三种可能:
    1. 只包含’(’
    2. 先是一串’)’然后再是一串’(’
    3. 只包含’)’

    根据这三种可能可以分成三类:

    1.‘(’比‘)’多的

    2.‘(’和‘)’一样多的

    3.‘(’比’)‘少的
    然后,按照第一类,第二类,第三类的顺序放置串。然后对于一类串,我们按照’)’个数从小到大排。三类串的串,我们按照’(‘个数从大到小排。对于排序完的串,我们从前往后扫一遍就行了。

    代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int MAXN = 1e5+50;
    
    struct D{
        int l,r;//处理后左右括号的数量 
        int flag;//代表所属类 
        bool operator < (const struct D &b)const{
            if(flag == b.flag){
                if(flag < 0)return l > b.l;
                else if(flag >= 0) return r < b.r;
            }
            else return flag > b.flag;
        }
    }board[MAXN];
    
    char S[MAXN];
    char Stack[MAXN];
    int tot;
    
    int Solve(int x){
        tot = 0;
        int sum = 0;
        int len = strlen(S);
        for(int i=0 ; i<len ; ++i){
            if(S[i] == '('){
                Stack[++tot] = S[i];
            }
            else if(tot && Stack[tot]=='('){
                --tot;
                sum += 2;
            }
            else Stack[++tot] = S[i];
        }
        board[x].l = board[x].r = 0;
        while(tot){
            if(Stack[tot] == '(')board[x].l++;
            else board[x].r++;
            --tot;
        }
        if(board[x].l > board[x].r)board[x].flag = 1;
        else if(board[x].l == board[x].r)board[x].flag = 0;
        else board[x].flag = -1;
        return sum;
    }
    
    int main(){
        
        int T,N;
        scanf("%d",&T);
        while(T--){
            int sum = 0;
            scanf("%d",&N);
            for(int i=1 ; i<=N ; ++i){
                scanf("%s",S);
                sum += Solve(i);
            }
            sort(board+1,board+1+N);
            int L = 0;
            for(int i=1 ; i<=N ; ++i){
            	int t = min(L,board[i].r);
            	L -= t;
                sum += 2*t;
                L += board[i].l;
            }
            printf("%d
    ",sum);
        }
        
        return 0;
    }
    
    
  • 相关阅读:
    Centos 环境变量
    Centos 多线程下载工具-axel
    【Sprint3冲刺之前】项目可行性研究报告
    【Sprint3冲刺之前】TDzhushou软件项目测试计划书
    【Sprint3冲刺之前】日历表的事件处理和管理(刘铸辉)
    【Sprint3冲刺之前】项目完成时间表
    【Sprint3冲刺之前】敏捷团队绩效考核(刘铸辉)
    【每日Scrum】第八天(4.29) TD学生助手Sprint2
    【每日Scrum】第七天(4.28)Sprint2总结性会议
    需求分析
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514036.html
Copyright © 2011-2022 走看看