zoukankan      html  css  js  c++  java
  • hdu 5831 Rikka with Parenthesis II

    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

     可以先写出一个符合条件的序列,然后交换其中一组括号,会发现正确的策略是交换第一个出现的右括号和最后一个出现的左括号。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5 + 10;
    char s[maxn];
    int main() {
        int t;
        scanf("%d", &t);
        while(t--) {
            int n;
            scanf("%d", &n);
            scanf("%s", s + 1);
            int a, b;
            a = b = -1;
            for(int i = 1; i <= n; i++) {
                if(s[i] == ')') {
                    a = i;
                    break;
                }
            }
            for(int i = n; i >= 1; i--) {
                if(s[i] == '(') {
                    b = i;
                    break;
                }
            }
            swap(s[a], s[b]);
            if(a == -1 || b == -1) puts("No");
            else {
                stack<char> stk;
                int flag = 1;
                for(int i = 1; i <= n; i++) {
                    if(s[i] == '(') stk.push(s[i]);
                    else {
                        if(!stk.empty())
                            stk.pop();
                        else {
                            flag = 0;
                            break;
                        }
                    }
                }
                if(stk.empty() && flag) puts("Yes");
                else puts("No");
            }
        }
    }
  • 相关阅读:
    深入理解JavaScript中的this关键字
    使用yii的layout,加入<?php echo $content; ?>这句话时,它会自动在子页面上面添加一个div包裹
    使用 BEGINCONTENT() 和 ENDCONTENT() 设定 YII 的 LAYOUTS
    孙悟空的师傅是谁
    PHP MemCached高级缓存配置图文教程
    C++中的基类与派生类
    矩阵快速幂
    华为上机测试题
    2016网易研发题目
    冒泡排序
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5767296.html
Copyright © 2011-2022 走看看