Balanced Sequence ( 括号最大匹配个数)

Chiaki has nn strings s1,s2,…,sns1,s2,…,sn consisting of '(' and ')'. A string of this type is said to be balanced: 

+ if it is the empty string 
+ if AA and BB are balanced, ABAB is balanced, 
+ if AA is balanced, (A)(A) is balanced. 

Chiaki can reorder the strings and then concatenate them get a new string tt. Let f(t)f(t) be the length of the longest balanced subsequence (not necessary continuous) of tt. Chiaki would like to know the maximum value of f(t)f(t) for all possible tt. 

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case: 
The first line contains an integer nn (1≤n≤1051≤n≤105) -- the number of strings. 
Each of the next nn lines contains a string sisi (1≤|si|≤1051≤|si|≤105) consisting of `(' and `)'. 
It is guaranteed that the sum of all |si||si| does not exceeds 5×1065×106. 

Output

For each test case, output an integer denoting the answer. 

Sample Input

2
1
)()(()(
2
)
)(

Sample Output

4
2

 

寻找最大平衡串的长度 

排序是关键

因为剩下的括号最多只有四种情况就是

1.只有(   ((((((((

2.只有)   )))))))

3.(比)多  (((((())

4.)比(多   ))))))(((

然后我们要做的就是把他们排序,既然要得到尽可能多的匹配,那就贪心去排序,每次将(放在前面,把)放在后面,然后排序规则可以这样

1.先把 ( 全放进来

2.把 ( 比 ) 多的串放在前面

3.如果两个串都是 ( 比 ) 多,那就把 ) 少的放在前面

4.如果两个都是 ( 比 ) 少,那就把 ( 多的放在前面

5.最后再把全部都是 ) 的放在后面。

然后在扫一遍,L记录我扫到这个串的时候还有多少 ( 没有匹配,然后每次尽可能多的拿去匹配掉

 

 

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include <queue>
using namespace std;
long long int sum;
struct ss
{
    int l;
    int r;
    int ans;
}str[100010];
bool cmp (ss x,ss y)
{
    ///')'比'('多的放后面
    if (x.r >= x.l && y.r < y.l)
        return false;
    if (x.r < x.l && y.r >= y.l)
        return true;
    ///')'比'('少   比较')'少的放前面
    /// ')'比'('多   比较'('少的放后面
    if (x.r < x.l && y.r < y.l)
        return x.r<y.r;
    else return x.l>y.l;
}
char s[100005];
int main()
{
    int n , t;
    int i , j, k;
    scanf("%d", &t);
    while( t-- )
    {
        sum = 0;
        memset( str, 0, sizeof( str ));
        scanf("%d",&n);
        for( i = 0; i<n; i++)
        {
            scanf("%s", s);
            int h = strlen( s );
            for( j = 0; j<h; j++)
            {
                if( s[j] == '(')
                {
                    str[i].l++;
                }
                else
                {
                   if( str[i].l > 0 )
                    {
                       str[i].l--;
                       str[i].ans++;
                    }
                    else
                        str[i].r++;
                }
            }
        }
        sort( str, str+n, cmp);
        long long int cnt = 0;
        for( i = 0; i<n; i++)
        {
            sum = sum+str[i].ans;
            if( cnt > 0 )
            {
                if( str[i].r >= cnt )
                {
                    sum = sum + cnt;
                    cnt = 0 ;
                }
                else
                {
                    sum = sum + str[i].r;
                    cnt  = cnt - str[i].r;
                }
                cnt = cnt+str[i].l;
            }
            else
            {
                cnt = cnt+str[i].l;
            }

        }
        printf("%lld\n", sum*2);
    }
       return 0;
}
以下是一个使用栈来判断括号匹配的C语言程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define STACK_SIZE 100 //定义栈结构体 typedef struct { char data[STACK_SIZE]; int top; } Stack; //初始化栈 void initStack(Stack *s) { s->top = -1; } //判断栈是否为空 int isEmpty(Stack *s) { return s->top == -1; } //判断栈是否已满 int isFull(Stack *s) { return s->top == STACK_SIZE - 1; } //入栈 void push(Stack *s, char c) { if (isFull(s)) { printf("Stack is full.\n"); exit(1); } s->data[++s->top] = c; } //出栈 char pop(Stack *s) { if (isEmpty(s)) { printf("Stack is empty.\n"); exit(1); } return s->data[s->top--]; } //获取栈顶元素 char peek(Stack *s) { if (isEmpty(s)) { printf("Stack is empty.\n"); exit(1); } return s->data[s->top]; } //判断括号是否匹配 int isMatch(char left, char right) { if (left == '(' && right == ')') return 1; else if (left == '[' && right == ']') return 1; else if (left == '{' && right == '}') return 1; else return 0; } //判断整个字符串中的括号是否匹配 int isBalanced(char *s) { Stack stack; initStack(&stack); int len = strlen(s); for (int i = 0; i < len; i++) { char c = s[i]; if (c == '(' || c == '[' || c == '{') { push(&stack, c); } else if (c == ')' || c == ']' || c == '}') { if (isEmpty(&stack)) return 0; char left = pop(&stack); if (!isMatch(left, c)) return 0; } } return isEmpty(&stack); } int main() { char s[100]; printf("Enter a string: "); scanf("%s", s); if (isBalanced(s)) { printf("The string is balanced.\n"); } else { printf("The string is not balanced.\n"); } return 0; } ``` 程序先定义了一个栈结构体,并实现了相关的栈操作函数。其中,isMatch函数用于判断左右括号是否匹配,isBalanced函数用于判断整个字符串中的括号是否匹配。 在main函数中,程序首先读入一个字符串,然后调用isBalanced函数判断该字符串中的括号是否匹配,并输出相应的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值