括号配对

  题目:Bracket Sequence

Description
Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence.2. If S is a regular sequence, then (S) is a regular sequence.3. If A and B are regular sequences, then AB is a regular sequence. For example, these sequences of characters are regular brackets sequences: (), (()), ()(), ()(()).And all the following character sequences are not: (, ), )(,()), (()().A sequence of characters '(' and ')' is given. You can insert only one '(' or ')' into the left of the sequence, the right of
the sequence, or the place between any two adjacent characters, to change this sequence to a regular brackets sequence.
Input
The first line has a integer T (1 <= T <= 200), means there are T test cases in total. For each test case, there is a sequence of characters '(' and ')' in one line. The length of the sequence is in range [1, 105].
Output
For each test case, print the number of places, into which you insert a '(' or ')', can change the sequence to a regular brackets sequence. What's more, you can assume there has at least one such place. 

Sample Input
4
)
())
(()(())
((())())(()

Sample Output

1
3
7
3

题目大概的意思就是说整个括号只要再加一个就可以完全的配对,输出有几个位置可以满足配对的条件的。

   

      解题报告:

      如果我们要找到整个序列中哪里不能够配对,我们要怎么做呢。有种方案就是使用栈来做,当出现一个左括号的时候我们就把括号压到栈里面,如果出现一个右括号,那么我们就把栈里面的左括号弹出来。什么时候会发生不配对的情况呢,当栈中的数量出现负数的时候,或者当全部的括号都读取完了,栈中的数量出现正数的时候,我们就判断这个括号不配对了。

      所以,我们以栈为基础,换个思路,设置一个数字用来统计当前左括号的数量,这样就可以方便的找到不匹配的地方。

      但是,这样只能找到哪里有问题,怎么要才能找到有多少个位置可以完成匹配的呢?

      我们拿上面的输入样例举例:())   0(起始) 1 0 -1 出现-1的时候我们发现这个序列就有问题了,但是到底有多少给位置可以纠正这个序列呢。我们想想,怎么样的序列才是正确的?当所有的序列的数字都是正数或0的时候,就能保证序列式正确的,因为题目确保了只会出现一个地方有问题,所以我们看下上面的这个例子。 1 0 -1 如果我们在任意一个位置加了左括号,那么会出现什么情况?比如我们在0前面加一个左括号。那么数字就会变成 1 2 1 0,那么这样是不是就符合了呢?所以,总结下,如果出现缺少左括号的情况,那么只要在第一次出现负数的情况下载他左边的任意位置都是符合条件的。

      但是出现缺少右括号的时候,那该怎么办?有一种想法是,将所有的序列倒置,左括号改成右括号,右括号改成左括号,那么情况就和之前的一样了。我的想法是,在出现最后一次0的情况下和最后为整数的位置之间的任意位置都是可以的。

      (()(()) 上述的值变化分别为0(起始) 1 2 1 2 3 2 1 最后一次出现0的就是在初始位置,然后同理在任意位置加了一个右括号以后,那么相当于就是说这个位置以后的每个数都要减1,因为是最后一个0,后面不会再出现0的情况,因此每个数都将>=0。


下面就是我自己写的一段代码。


#include<stdio.h>
#include<string.h>
char aa[100100];
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=0;t<T;t++)
    {
        scanf("%s",aa);
        int length=strlen(aa);
        aa[length]='\0';
        int count=0,queyou=0,i=0;
        for(;i<length;i++)
        {
            if(count<0)
            {
                break;
            }else{
                if(aa[i]=='(')
                count++;
                else
                count--;
                if(count==0)
                {
                    queyou=i+1;
                }
            }
        }
        if(count<0)
        printf("%d\n",i);
        else
        printf("%d\n",length-queyou);
    }
    return 0;

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值