Rikka with Parenthesis II(括号匹配)

Problem Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Correct parentheses sequences can be defined recursively as follows:
1.The empty string “” is a correct sequence.
2.If “X” and “Y” are correct sequences, then “XY” (the concatenation of X and Y) is a correct sequence.
3.If “X” is a correct sequence, then “(X)” is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include “”, “()”, “()()()”, “(()())”, and “(((())))”.

Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different position i,j and swap Si,Sj.

Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.

It is too difficult for Rikka. Can you help her?

Input

The first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100

For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.

Output

For each testcase, print “Yes” or “No” in a line.

Sample Input

3

4

())(

4

()()

6

)))(((

Sample Output

Yes

Yes

No

Hint
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
题目大意:

给你n长度的一个括号串,问你是否能够在必须交换两个括号的情况下,使得最终交换得到的括号串是匹配的。

思路:

1、首先,需要明确一点,如果n%2 = =1,输出No,括号中的左括号和右括号的数目不同,输出No。因为必须挪动,所以我们n==2的时候要特判一下。

2、如果n%2= =0&&左括号数目==右括号数目,那么我们遍历一遍字符串,如果遇到左括号,sum++,记录左括号的数目.如果遇到右括号,如果sum大于0,那么sum–。否则讨论:从第一个位子到当前位子作为一个子串的话,其子串一定做不到所有括号都匹配,所以这个括号是一定需要挪动的,那么我们问题转化:统计这样必须挪动的括号的个数。

3、那么对于这种需要挪动的括号进行记数,如果这样的括号有0或者1或者2个,都是可行的:

①0个需要挪动的括号,而且n>=4,那么随便挪动一对括号就行。

②1个需要挪动的括号,那么一定有一个右括号也需要挪动与之匹配:)(

③2个需要挪动的括号,那么一定有两个右括号也需要挪动与之匹配:
))((挪动14就可以达到目的.

#include<bits/stdc++.h>
using namespace std;
const int maxx = 1e5+100;
char a[maxx];
int main()
{
    int t;
    int n;
    scanf("%d", &t);
    while(t--)
    {
        stack<char> q;
        scanf("%d", &n);
        scanf("%s", a);
        if(n%2!=0)
        {
            printf("No\n");
            continue;
        }
        if(n==2&&a[0]=='('&&a[1]==')')
        {
            printf("No\n");
            continue;
        }
        for(int i=0; i<n; i++)
        {
            if(a[i]=='(')
                q.push(a[i]);
            else if(a[i]==')')
            {
                if(!q.empty()&&q.top()=='(')
                    q.pop();
                else
                    q.push(a[i]);
            }
        }
        if(q.empty())
            printf("Yes\n");
        else if(q.size()!=2&&q.size()!=4)
            printf("No\n");
        else
        {
            char s[10];
            if(q.size()==2)
            {
                s[1] = q.top();
                q.pop();
                s[0] = q.top();
                if(s[1]=='('&&s[0]==')')
                    printf("Yes\n");
                else
                    printf("No\n");
            }
            else
            {
                for(int i=3; i>=0; i--)
                {
                    s[i] = q.top();
                    q.pop();
                }
                if(s[3]=='('&&s[2]=='('&&s[1]==')'&&s[0]==')')
                    printf("Yes\n");
                else
                    printf("No\n");
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值