UVa 673 - Parentheses Balance

来源:互联网 发布:塘厦cnc编程招聘 编辑:程序博客网 时间:2024/04/25 13:54

题目:括号匹配的合法判断。

分析:简单题、栈。

注意:输出空行。

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stack> using namespace std;string str;int main(){int n;while ( cin >> n ) {getchar();while ( n -- ) {getline(cin,str);stack<char>S;int L = str.length();int e = 0;int flag = 1;for ( int i = 0 ; i < L ; ++ i ) {if ( str[i] == '(' || str[i] =='[' )S.push(str[i]);if ( str[i] == ']' ) {if ( !S.empty() && S.top() == '[' )S.pop();else {flag = 0;break;}}if ( str[i] == ')' ) {if ( !S.empty() && S.top() == '(' )S.pop();else {flag = 0;break;}}}if ( flag && S.empty() )cout << "Yes" << endl;else cout << "No" << endl;}}return 0;}