zoukankan      html  css  js  c++  java
  • Uva673 Parentheses Balance

    题目链接:https://vjudge.net/problem/UVA-673

    这道题并不难解, 用一个栈保存,只要遇到 '(' 和 '[' 入栈,然后遇到 ']' ')' 匹配便出栈,若最后栈不空则未空

    但这题有个很严重的问题,需要判断空行,一开始笔者用了scanf("%s", s) 一直在wa,后来发现必须用fgets(),并且注意在fgets前用一个getchar读取第一个数字前的

    回车

    代码

    #include <cstdio>
    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    #include <bitset>
    #include <queue>
    #include <memory.h>
    #include <map>
    #include <cmath>
    #include <stack> 
    #include <string>
    #include <cstring>
    #include <cassert>
    #define ll long long
    using namespace std;
    
    const int MAX = 10010;
    
    bool isCorrect(const char *s){
        int len = strlen(s)-1;
        stack <char> sta;
        for(int i = 0; i < len; i++){
            char c = s[i];
            if(c == '[' || c == '(') sta.push(c);
            else{
                if(sta.empty()) return false;
                char x = sta.top();
                 if(c == ')'){
                    if(x == '(') sta.pop();
                    else break; 
                }
                else if(c == ']'){
                    if(x == '[') sta.pop();
                    else break;
                }
            }
        }
        return sta.empty();
    }
    
    
    int main(){
        int T;
        char s[MAX]; 
        scanf("%lld", &T);
        getchar();
        while(fgets(s, sizeof(s), stdin) != NULL){
    //        if(strlen(s) == 0) puts("Yes");
            if(isCorrect(s)) puts("Yes");
            else puts("No");
        }
    }
  • 相关阅读:
    Alpha冲刺——第六天
    Alpha冲刺——第五天
    第一次课 圆面积
    计算器第四次作业
    计算器稍微小结
    附加作业3
    2016寒假训练4
    2016寒假训练3
    第三次作业
    2016寒假训练2
  • 原文地址:https://www.cnblogs.com/Phoenix-blog/p/9460149.html
Copyright © 2011-2022 走看看