10 solutions

  • 0
    @ 2022-1-2 16:35:59

    眼睛疼

    using namespace std; 
    int main(){
    	int n;
    	char s[256];
    	cin>>n;
    	while(n--){
    	stack<char>a;
    		scanf("%s",&s);
    		int k=0;
    		int len=strlen(s);
    		for(int i=0;i<len;i++){
    			if(s[i]=='{'){
    				if(a.empty()||a.top()=='{'){
    				a.push(s[i]);
    				}
    				else{
    					cout<<"NO\n";
    					k=1;
    					break;
    				}
    			}
    			else if(s[i]=='['){
    				if(a.empty()||a.top()=='{'||a.top()=='['){
    					a.push(s[i]);
    				}
    				else{
    					cout<<"NO\n";
    					k=1;
    					break;
    				}
    			}
    			else if(s[i]=='('){
    				if(a.empty()||a.top()=='{'||a.top()=='['||a.top()=='('){
    					a.push(s[i]);
    				}
    				else{
    					cout<<"NO\n";
    					k=1;
    					break;
    				}
    			}
    			else if(s[i]=='<'){
    				if(a.empty()||a.top()=='{'||a.top()=='['||a.top()=='('||a.top()=='<'){
    					a.push(s[i]);
    				}
    				else{
    					cout<<"NO\n";
    					k=1;
    					break;
    				}
    			}
    			else if(s[i]=='>'){
    				if(!a.empty()&&a.top()=='<'){
    					a.pop();
    				}
    				else{
    					cout<<"NO\n";
    					k=1;
    					break;
    				}
    			}
    			else if(s[i]==')'){
    				if(!a.empty()&&a.top()=='('){
    					a.pop();
    				}
    				else{
    					cout<<"NO\n";
    					k=1;
    					break;
    				}
    			}
    			else if(s[i]==']'){
    				if(!a.empty()&&a.top()=='['){
    					a.pop();
    				}
    				else{
    					cout<<"NO\n";
    					k=1;
    					break;
    				}
    			}
    			else if(s[i]=='}'){
    				if(!a.empty()&&a.top()=='{'){
    					a.pop();
    				}
    				else{
    					cout<<"NO\n";
    				k=1;
    				break;
    				}
    			}
    		}
    	if(a.empty()&&k==0) cout<<"YES\n";
    	else if(k==0)cout<<"NO\n";
    	}
        return 0;
    }
    

    Information

    ID
    296
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    7
    Tags
    # Submissions
    176
    Accepted
    36
    Uploaded By