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");
            }
        }
    }
  • 相关阅读:
    Android 架构 1.架构
    Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式
    RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第三篇【实例】
    RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第二篇【原理】
    RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第一篇【安装】
    选择日期控件
    robot Framework控制浏览器
    botot framework选择下拉框
    soapui测试接口使用步骤
    使用python实现appium的屏幕滑动
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5767296.html
Copyright © 2011-2022 走看看