hdu 5831 Rikka with Parenthesis II (可交换位置括号匹配)

Rikka with Parenthesis II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 106 Accepted Submission(s): 81

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.

题意:给你一个由 ( )组成的字符串,然后你必须交换一对不同位置字符,问你最后是否可以括号匹配

思路:我们考虑普通的括号匹配,因为必须需要交换,所以说我们可以分情况讨论,前提是,这个字符串两种字符的数量相同:
1.当前字符串已经括号匹配,那么检查匹配数,如果 1 ,必定不会成功,当 >1 的时候,可以成功。
2.当前字符串没有括号匹配,那么还是检查匹配数,如果 括号匹配应有的匹配数-2,那么是可以的,否则不行。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-11-12.18 Thursday
File Name    : D:\MyCode\2016-8月\2016-8-11.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
char str[MAXN];
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int len;scanf("%d",&len);
        scanf("%s",str);
        int numl=0,numr=0;
        for(int i=0;i<len;i++)
        {
            if(str[i]=='(')
                numr++;
            else
                numl++;
        }
        if(numl!=numr)
        {
            printf("No\n");
            continue;
        }
        stack<char>s;int cnt=0;
        for(int i=0;i<len;i++)
        {
            if(str[i]=='(')
            s.push(str[i]);
            else
            {
                if(s.empty())
                    continue;
                char b=s.top();
                if(b=='('&&str[i]==')')
                {
                    s.pop();
                    cnt++;
                }
            }
        }
        if(cnt==len/2)
        {
            if(cnt>1) printf("Yes\n");
            else printf("No\n");
        }
        else
        {
            if(cnt>=len/2-2)
                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、付费专栏及课程。

余额充值