1 条题解

  • 0
    @ 2023-6-11 12:17:13

    C++ :

    #include <bits/stdc++.h> 
    using namespace std; 
    
    string s;
    //检验是否匹配 
    bool check(){
    	stack<char> p;//定义能够存储char的栈
    	p.push('#');//先存一个#,避免空栈判断
    	
    	for(int i = 0;i < s.size();i++){
    		char c = s[i];
    		if(c == ')'){
    			if(p.top() != '(')
    			   return false;
    			else
    			   p.pop();
    		}else if(c == ']'){
    			if(p.top() != '[')
    			   return false;
    			else 
    			   p.pop();
    		}else{
    			p.push(c);
    		}
    	}
    	
    	return (p.size() == 1);
    } 
    
    int main(){ 
    	cin>>s;
    	if(check()){
    		cout<<"yes"<<endl;
    	}else{
    		cout<<"no"<<endl;
    	}
    	return 0; 
    } 
    
    
    • 1

    信息

    ID
    2406
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者