习题6 -1 Parentheses Balance uva673 简单栈的应用

来源:互联网 发布:淘宝搜索引擎简称 编辑:程序博客网 时间:2024/05/22 17:42

题目链接:https://vjudge.net/problem/UVA-673#author=0

Code:

#include <bits/stdc++.h>using namespace std;int main(){int T;cin >> T;getchar();while( T-- ){stack<char>s;string line;getline(cin,line);char ch;int spa = 1;for( int i = 0 ; i < line.size() ; i++ ){ch = line[i];if( ch == '(' || ch == '[' ) s.push(ch);else if( !s.empty() && s.top() == '(' && ch == ')' ) s.pop();else if( !s.empty() && s.top() == '[' && ch == ']' ) s.pop();else spa = 0; }if( spa && !s.size() ) cout << "Yes" << endl;else cout << "No" << endl;}return 0;}

原创粉丝点击