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");
            }
        }
    }
  • 相关阅读:
    centos 7安装配置vsftpd
    lvs和haproxy机器必须注意的三个参数
    用python 脚本实现zabbix对java端口报警
    git的下载地址
    看的一篇很好的博客
    学习内容
    数组一些用法
    dom
    dom1
    for的基础
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5767296.html
Copyright © 2011-2022 走看看