zoukankan      html  css  js  c++  java
  • ACM 括号配对问题

    括号配对问题

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    现在,有一行括号序列,请你检查这行括号是否配对。
     
    输入
    第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
    输出
    每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
    样例输入
    3
    [(])
    (])
    ([[]()])
    样例输出
    No
    No
    Yes

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <stack>
    using namespace std;
    
    int main(){
        int n;
        cin >> n;
        while(n--){
            string str;
            cin >>str;
            stack<char> branch;
            for(int i = 0 ; i < str.length();++ i){
                if(branch.empty()) branch.push(str[i]);
                else if(str[i] == ']'|| str[i] == ')'){
                    if(str[i] == ']' && '[' == branch.top() ) branch.pop();
                    else if(str[i] == ')' && '(' == branch.top()) branch.pop();
                    else break;
                }else{
                    branch.push(str[i]);
                }
            }
            if(branch.empty()) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
  • 相关阅读:
    进程的经典同步问题
    数学余数在计算机的用途
    7.货仓选址 绝对值不等式
    6. 排队打水 排序不等式
    5.合并果子 Huffman树
    4.区间覆盖 区间问题
    3.区间分组 区间问题
    2.最大不相交区间数量 区间问题
    1.区间选点 区间问题
    26.拆分-Nim游戏 博弈论
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3672972.html
Copyright © 2011-2022 走看看