• HDU 5831 Rikka with Parenthesis II (贪心)


    Rikka with Parenthesis II

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 0    Accepted Submission(s): 0

    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

    Hint

    For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.

    题目大意:

    给你n长度的一个括号串,问你是否能够在必须交换两个括号的情况下,使得最终交换得到的括号串是匹配的

    题解:

    若左括号和右括号数量不等,输出’No’

    若n==2,这个串刚好是’()’,因为必须交换的原因,也是’No’。

    其它情况:

    ①0个需要挪动的括号,而且n>=4,那么随便挪动一对括号就行。

    ②1个需要挪动的括号,那么一定有一个右括号也需要挪动与之匹配:)(

    ③2个需要挪动的括号,那么一定有两个右括号也需要挪动与之匹配:))((挪动14就可以达到目的.

    若大于等于三队,则是’No’。(采用栈的方法判断括号的匹配情况) 

    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    
    using namespace std;
    int T,n,l,r;
    char ch[100005];
    stack<char>s;
    int main()
    {
        scanf("%d",&T);
        for(;T>0;T--)
        {
            scanf("%d",&n);
            scanf("%s",&ch);
            if (n==2 && ch[0]=='(' && ch[1]==')')
            {
                printf("No
    ");
                continue;
            }
            while(!s.empty()) s.pop();
            l=0; r=0;
            for(int i=0;i<n;i++)
            {
                if (ch[i]=='(') s.push(ch[i]);
                 else
                 {
                     if (!s.empty()) s.pop();
                      else l++;
                 }
            }
            r=s.size();
            if (r!=l) {printf("No
    "); continue;}
            if (l>2) {printf("No
    "); continue;}
            printf("Yes
    ");
        }
        return 0;
    }
  • 相关阅读:
    最短路径—Dijkstra算法和Floyd算法
    设计模式之工厂模式(Factory模式)
    接口继承与实现继承
    设计模式之工厂模式
    C++的四种强制转型形式:
    手写atoi、strcpy、strcat
    进程和线程关系及区别
    海量数据处理
    什么是死锁,简述死锁发生的四个必要条件,如何避免与预防死锁
    kolla-ansible 重新部署 ceph 遇到的问题
  • 原文地址:https://www.cnblogs.com/stepping/p/7204373.html
Copyright © 2020-2023  润新知