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");
            }
        }
    }
  • 相关阅读:
    存储器结构、cache、DMA架构分析--【原创】
    驱动开发--【字符设备、块设备简介】【sky原创】
    如何在博客园上面显示自己定义的头像--【sky原创】
    程序执行的过程分析--【sky原创】
    如何在linux下检测内存泄漏(转)
    register 用法注意与深入--【sky原创】
    volatile的深入理解--【sky原创】
    【sky第二期--PID算法】--【智能车论坛】
    滤波电容的选择
    实现开发板与ubuntu的共享--根文件系统NFS--Samba共享【sky原创】
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5767296.html
Copyright © 2011-2022 走看看