HDU - 6299 Balanced Sequence (greedy)

The meaning of problems : you just containing the n '(' ') the string', you can free ends of the right and left connecting these strings into a new string, then the condition can be determined in line with the longest sequence obtained.
Conditions: (1) comply with the empty string (2) if A is in line with (A) is also in line (3) if A, B in line with the line with AB: In sum this bracket is placed on the stack the string, if it can eject all It was qualified string (left and right brackets match will pop up)
thinking : This question is about, at the beginning may not think greed. (Read the questions are read from closing ..) but later will find the longest sequence to make, we certainly hope that the left and right parentheses match in a recent string of
such) ( ( ) ( ( ) , we certainly hope 2 4 and 7 matches rather than match, i.e., the right as long as the two strings match count i.e. it to the longest string in the sequence.

This way we get this done good in parentheses after the match only) ((above the sample string. That string matching any case after the last remaining (or not) of the left and right parentheses appear

The next step is to discuss as there are more strings) (This form how best match first by the idea of ​​greedy, we certainly want two strings left and right sides as close as possible so as not to waste brackets.

When we match two adjacent strings, such as a:))) (b:) ((, we will find more about which side brackets: a right parenthesis is greater than the number of left parenthesis, b parenthesis is greater than the number of left and right brackets, so greatly match, the two will swap places

If a:))) ((b:)) (right parenthesis on both sides is smaller than the left parenthesis, the number of two strings we want right parenthesis or left parenthesis more certainly contrary Similarly matching (that is, the right than the left parenthesis. brackets number of cases)

So this way we get greedy sort:

 if(a.l > a.r) return b.l > b.r? a.r < b.r : true;
 if(a.l <= a.r) return b.l <= b.r? a.l > b.l :  false;

code:

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = 1e5+10;
struct node{
    int l,r;
}p[maxn];
bool cmp(node a,node b){
    if(a.l > a.r) return b.l > b.r? a.r < b.r : true;
    if(a.l <= a.r) return b.l <= b.r? a.l > b.l :  false;
}
char s[maxn];
int main(){
    int n;
    int T;
    scanf("%d",&T);
    while(T--){
       scanf("%d",&n);
       int ans = 0;
       for(int i = 1;i <= n;i++){
            scanf("%s",s);
            int len = strlen(s);
            p[i].l = p[i].r = 0;
            for(int j = 0;j < len;j++){
                if(s[j] == '(') p[i].l++;
                else{
                    if(p[i].l > 0){
                        p[i].l--;
                        ans += 1;
                    }
                    else{
                        p[i].r++;
                    }
                }
            }
       }
        sort(p+1,p+n+1,cmp);
        int l=0;
        for(int i=1; i<=n; i++)
        {
            if(p[i].r<=l)
                ans+=p[i].r,l-=p[i].r;
            else ans+=l,l=0;
            l += p[i].l;
        }
        printf("%d\n",ans*2);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11356784.html