uva 673

一看就知道用栈模拟,如果题目中有除了括号外的其他字符,把is_ok函数加上即可

#include <iostream> using namespace std ; const int maxn = 128 + 10 ; struct { char str[maxn] ; int top ; } Stack ; char str[maxn] ; void init() { memset( str , 0 , sizeof( str ) ) ; memset( Stack.str , 0 , sizeof( Stack.str ) ) ; Stack.top = 0 ; return ; } bool can_match( char c1 , char c2 ) { if( ( c1 == '(' && c2 == ')' )|| ( c1 == '[' && c2 == ']' ) ) return true ; return false ; } //bool is_ok( char c ) //{ // if( c == '(' || c == ')' || c == '[' || c == ']' ) return true ; // return false ; //} int main() { int ncase ; cin >> ncase ; getchar() ; for( int icase = 0 ; icase < ncase ; icase++ ) { init() ; //if( icase ) cout << endl ; gets( str ) ; int len = strlen( str ) ; if( len % 2 ) { cout << "No" << endl ; continue ; }//只有是偶数的时候才有可能是yes int flag = 0 ;//如果是空,flag为0 for( int i = 0 ; i < len ; i++ ) { flag = 1 ; if( can_match( Stack.str[Stack.top] , str[i] ) ) Stack.top-- ; else //if( is_ok( str[i] ) ) { Stack.top++ ; Stack.str[Stack.top] = str[i] ; } } //cout << str << endl ; if( flag ) { if( Stack.top ) cout << "No" << endl ; else cout << "Yes" << endl ; } else cout << "Yes" << endl ; } return 0 ; }

你可能感兴趣的:(uva 673)