UVA 673 Parentheses Balance

来源:互联网 发布:mac os x sierra壁纸 编辑:程序博客网 时间:2024/05/01 06:57

题目

括号平衡

分析

简单的栈的运用,注意输入空行时的处理。

代码

#include <cstdio>#include <cstring>#include <stack>using std::stack;bool solve(char* s, int l){    stack<char> p;    for (int i = 0; i < l; i++) {        if (*(s+i) == '(' || *(s+i) == '[') {            p.push(*(s+i));        } else if (*(s+i) == ')') {            if (!p.empty() && p.top() == '(') p.pop();            else return false;         } else {            if (!p.empty() && p.top() == '[') p.pop();            else return false;         }    }    return p.empty();}int main(){    int t;    char buf[130];    scanf("%d\n", &t);    while (t--) {        fgets(buf, 130, stdin);        printf(solve(buf, strlen(buf)-1) ? "Yes\n" : "No\n");    }    return 0;}
0 0
原创粉丝点击