zoukankan      html  css  js  c++  java
  • Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题

    C. Bad Sequence

    Problem Description:

      Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself.
      To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning "(" into ")" or vice versa) isn't allowed.
    We remind that bracket sequence s is called correct if:

    1. s is empty;
    2. s is equal to "(t)", where t is correct bracket sequence;
    3. s is equal to t1t2, i.e. concatenation of t1 and t2, where t1 and t2 are correct bracket sequences.

    For example, "(()())", "()" are correct, while ")(" and "())" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct.
    Input
    First of line of input contains a single number n (1≤n≤200000) — length of the sequence which Petya received for his birthday.
    Second line of the input contains bracket sequence of length n, containing symbols "(" and ")".
    Output
    Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "No".

    Input1

    2
    )(

    Output1

    Yes

    Input2

    3
    (()

    Output2

    No

    Input3

    2
    ()

    Output3

    Yes

    题意:给出字符串长度,和一段只含左右括号的字符,并定义该字符序列是好的条件为括号匹配或者只通过移一个括号,能使其完全匹配,如果满足上述条件,则输出Yes,否则输出No。

    思路:用栈模拟括号匹配.最后判断栈中元素是否只有  )  (   这 两种括号即可.

    AC代码:

    #include<bits/stdc++.h>
     
    using namespace std;
     
    int main(){
        int n;
        cin>>n;
        string str;cin>>str;
        stack<char> s;
        if(n%2){
            printf("No");return 0;
        }
        for(int i=0;i<n;i++){
            if(s.empty()){
                s.push(str[i]);
            }else{
                if(str[i]==')'){
                    char temp=s.top();
                    if(temp=='('){
                        s.pop();
                    }else{
                        s.push(str[i]);
                    }
                }else{
                    s.push(str[i]);
                }
            }
        }
        if(s.empty()){
            printf("Yes
    ");return 0;
        }else{
            if(s.size()!=2){
                printf("No");return 0;
            }else{
                char t1=s.top();s.pop();
                char t2=s.top();s.pop();
                if(t1=='('&&t2==')'){
                    printf("Yes
    ");
                }else{
                    printf("No
    ");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    python-super方法
    python--mixin
    python中将输出写入文件
    一致性hash算法
    mysql之触发器trigger
    python内置函数
    python3.5+Django2.2+pymysql+mysql
    File "D:PythonPython37-32libsite-packagesdjangoviewsdebug.py", line 332, in get_traceback_html   t = DEBUG_ENGINE.from_string(fh.read())
    RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to po
    python:面向对象
  • 原文地址:https://www.cnblogs.com/pengge666/p/11474474.html
Copyright © 2011-2022 走看看