VJ_Parentheses Balance_stack_or_s.erase()

 

// stack
#include<bits/stdc++.h>
using namespace std;

int main()
{
    stack<char> sk;
    string s,tt;
    int t,i;

    while( cin>>t )
    {
        cin.get();
        while( t-- )
        {
            while( !sk.empty() ) sk.pop();		// 多组数据 记得清空 
			getline( cin,s );
            
            for( i=0;i<s.size();i++ )
            {
                if( sk.empty() ) { sk.push( s[i] ); continue; }
                
				tt=sk.top();			// + 可能导致(int)+(int) 
				tt+=s[i];	
                if( tt=="()" || tt=="[]" ) sk.pop();
                else sk.push( s[i] );
            }
            if( sk.empty() )    cout<<"Yes"<<endl;
            else                cout<<"No"<<endl;
        }
    }
    return 0;
}

// string erase() 
#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    int f1,f2,t,pos;

    while( cin>>t )
    {
        cin.get();
        while( t-- )
        {
            getline( cin,s );

            f1=f2=1;
            while( f1 || f2 )       //   ||
            {
                f1=f2=1;
                pos=s.find( "()" );
                if( pos!=-1 ) s.erase( pos,2 );
                else f1=0;

                pos=s.find( "[]" );
                if( pos!=-1 ) s.erase( pos,2 );
                else f2=0;
            }
            if( s.empty() ) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_63173957/article/details/124382361