HDU 5831 Rikka with Parenthesis II (括號匹配)

Mr_Treeeee發表於2020-04-06


Rikka with Parenthesis II

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


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.
 

Author
學軍中學
 

Source
 

題意:
給你一個括號串,讓你必須交換一次,判斷括號串是否匹配。必須!

POINT:
因為必須,所以單獨拎出n=2且為()這種情況,顯然是No。
我把左右括號不相等的情況也拎了出來,這樣感覺比較好寫。
其他情況:
如果一開始就是匹配的,交換後還是匹配。
如果一開始不匹配,就讓第一個使串不匹配的)和最後一個(交換。這樣還是沒有匹配的話,就是No,反之Yes。
cnt[]陣列是用來判斷匹配的。賦值規則為:遇(加1,遇)減1,有小於0的肯定就不匹配了。
交換一次“)” “(”號,那麼交換的區間內cnt[]全部加2 即多了一個左括號,少了一個右括號,總加2。若還有小於0的就是匹配不了。


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <math.h>
using namespace std;
#define ll long long
const int N = 100000+4;
char s[N];
int cnt[N];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int now=0;
        int n;
        cin>>n;
        int flag=0;
        cin>>s;
        int you=999999;
        int zuo;
        int fz=0,fy=0;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='(')
            {
                now++;
                zuo=i;
                fz++;
            }
            else now--,fy++;
            if(now<0&&!flag)
            {
                flag=1;
                you=i;
            }
            cnt[i]=now;
        }
        int ans=1;
        if(fz!=fy) ans=0;
        for(int i=you;ans&&i<=zuo;i++)
        {
            if(cnt[i]+2<0)
            {
                ans=0;
                break;
            }
        }
        if(strlen(s)==2&&s[0]=='('&&s[1]==')') printf("No\n");
        else if(!ans) printf("No\n");
        else printf("Yes\n");
    }
    
}



相關文章