UVa 673 Parentheses Balance

UVa 673 Parentheses Balance
和上午做的某一题类似,比那一题还简单点。不过我却犯了同样的错误!
最终成功的条件不仅是没有遇到匹配失误,栈还必须为空!
以下是我的代码:
#include < iostream >
#include
< stack >
#include
< string >
#include
< cstdio >
using   namespace  std;

int  main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/

    
int  n;
    cin
>> n;
    cin.
get ();
    
for ( int  i = 1 ;i <= n;i ++ )
    {
        
string  t;
        getline(cin,t);

        stack
< char >  s;
        
bool  fail( false );
        
for ( int  j = 0 ;j < t.size();j ++ )
            
if (t[j] == ' ( '   ||  t[j] == ' [ ' )
                s.push(t[j]);
            
else   if (t[j] == ' ) ' )
            {
                
if (s.empty()  ||  s.top() != ' ( ' )
                {
                    fail
= true ;
                    
break ;
                }
                
else
                    s.pop();
            }
            
else   if (t[j] == ' ] ' )
            {
                
if (s.empty()  ||  s.top() != ' [ ' )
                {
                    fail
= true ;
                    
break ;
                }
                
else
                    s.pop();
            }

        
if ( ! fail  &&  s.empty())
            cout
<< " Yes " << endl;
        
else
            cout
<< " No " << endl;
    }

    
return   0 ;
}

你可能感兴趣的:(UVa 673 Parentheses Balance)