top1编程
← 返回题目
题解

【入门】括号匹配

1 条题解

  • 0
    @ 2026-7-28 22:44:44
    #include<bits/stdc++.h>
    using namespace std;
    string a,s;
    int top=0;
    int main()
    {
    	cin>>a;
    	for(int i=0;i<a.size();i++){
    		if(a[i]=='('||a[i]=='['){
    			s[++top]=a[i];
    		}
    		else if(a[i]==')'&&s[top]=='('&&top>0){
    			top--;
    		}else if(a[i]==']'&&s[top]=='['&&top>0){
    			top--;
    		}else{
    			cout<<"no";
    			return 0;
    		}
    	}
    	if(top==0){
    		cout<<"yes";
    	}else{
    		cout<<"no";
    	}
    	return 0;
    }
    
    • 1