HDU 5831 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.

数据结构练习括号匹配的升级版,对整个串进行括号匹配,最后剩下的只有

1.空栈

2.栈内串为")(" 或"))(("

时才能只交换一次,加上对n==0的特判,其余情况都是”No”;

注意必须要有操作,所以初始“()”结果是No;

ac代码:

#include 

using namespace std;

#define rep(i,a,n) for(int i = (a); i < (n); i++)
#define per(i,a,n) for(int i = (n)-1; i >= (a); i--)
#define clr(arr,val) memset(arr, val, sizeof(arr))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pi acos(-1)
typedef pair<int, int> pii;
typedef long long LL;
const int maxn = 1006;
const double eps = 1e-8;
const int mod = 1000000007;

int main(int argc, char const *argv[]) {
    int t;
    int n;
    string a;
    cin >> t;
    while (t--) {
        stack<char>s;
        char ans[1005];
        cin >> n;
        if(n == 0) {puts("Yes");continue;}
        cin >> a;
        int idx = 0;
        rep(i, 0, n){
            if(a[i] == '(') s.push(a[i]);
            else {
                if(!s.empty() && s.top() == '(') s.pop();
                else s.push(a[i]);
            }
        }
        bool f = false;
        if(s.empty() && n!=2) f = true;
        else {
            while (!s.empty()) {
                ans[idx++] = s.top()=='(' ? ')' :'(';
                s.pop();
            }
            ans[idx] = '\0';
            if(strlen(ans) == 2 && ans[0] ==')' && ans[1] == '(') f = true;
            if(strlen(ans) == 4 && ans[0] ==')' && ans[1] == ')' && ans[2] == '(' && ans[3] == '(') f = true;

        }
        puts(f ? "Yes" : "No");
    }
    return 0;
}

你可能感兴趣的:(胡搞,====数据结构====,=====思维=====)