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");
            }
        }
    }
  • 相关阅读:
    省市区多级联动数据组合
    常用字符串函数
    设计模式分类
    计算机网络学习笔记(绪论第一部分)
    关于JAVA核心技术(卷一)读后的思考(继承中的类、子类和超类)
    关于JAVA核心技术(卷一)读后的思考(用户自定义类,静态域和静态方法的思考以及方法参数)
    关于JAVA核心技术(卷一)读后的思考(对象与类,日历的构造)
    微型学生信息管理系统的建立
    PHP异常处理(Exception)
    使用trait中相同方法的优先级问题
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5767296.html
Copyright © 2011-2022 走看看