hdu 6299 Balanced Sequence(模擬棧, 貪心)

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

題意想必大家都理解,我就不多說了,,,

題的關鍵在於當左右括號匹配時,將已匹配的括號從數組中刪除,並且數量+2(看到這,是不是覺得和棧很像~),最後將每個字符串剩餘部分進行組合,達到匹配的括號最大化就可,比賽時我就卡在了這,主要是時間不多了,最後40分鐘纔開始看,,,

本來開始時我是直接用的棧,但我開始時沒想到如何建立棧組,,,不知怎麼標記該棧左右剩餘的個數及情況,,,emmm後來發現自己想多了,,,其實很簡單,只需再開一個數組,建立一個結構體,記錄一下左右括號數就行,,,

主要是因爲建立結構體後,我就否定了上面的棧, 感覺重新模擬似乎更方便,就乾脆直接都是模擬了

不過有一個地方我覺得我沒問題,但就是wa,我也不知爲何,,,十分難受,,,wa的代碼在本篇的最後,有哪位好心的大佬能幫幫我這個小渣渣嘛~

#include<cstdio>
#include<stack>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

#define clr(x) memset(x, 0, sizeof(x));
const int N = 1000010;
typedef long long ll;

struct node{
    int l, r, s;//分別代表左括號,右括號及該字符串最長平衡子串的長度
}a[N], b;

bool cmp(node x, node y){
    if(x.l > x.r && y.r >= y.l)   return 1;
    else if(x.l <= x.r && y.r < y.l)    return 0;
    else if(x.l > x.r && y.l > y.r)     return x.r < y.r;
    else return x.l > y.l;

}

int main(){
    int t, n;
    char c[N];
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        for(int i=0; i<n; i++){
            scanf("%s", c);
            //cout << i << ' ' <<c[i] << endl;
            int len = strlen(c);
            a[i].l = a[i].r = a[i].s = 0;
            for(int j = 0; j < len; j++){
                if(c[j] == '('){
                    a[i].l++;
                }
                else{
                    if(a[i].l > 0){
                        a[i].s += 2;
                        a[i].l--;
                    }
                    else{
                        a[i].r++;
                    }
                }
            }
            //cout << "a " << i << ' ' <<a.top() << endl;
        }
        sort(a, a+n, cmp);
        int l = 0, sum = 0;
        for(int i = 0; i < n; i++){
            sum += a[i].s;
            int r = a[i].r;
            if(l && r){
                int m = min(l, r);
                sum += 2*m;
                l-=m;
            }
            l += a[i].l;
        }
        printf("%d\n", sum);
    }
    //system("pause");
    return 0;
}

哪位大佬能幫忙解釋一下 我這個判斷爲什麼不對~哇~難受~qwq

 b.s = b.l = b.r = 0;
 for(int i = 0; i < n; i++){
    b.s += a[i].s;
           b.l += a[i].l;
    b.r += a[i].r;
}
 b.l -= a[n-1].l;
 b.r -= a[0].r;
// cout << b.l << ' ' << b.r << endl;
int m = min(b.l, b.r);
// cout << m << endl;
b.s += 2*m;
發佈了196 篇原創文章 · 獲贊 316 · 訪問量 200萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章