3 solutions

  • 2
    @ 2024-4-6 16:04:16
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        string s;
        cin>>s;
        stack<char> stk;
        map<char,int> h={{'(',1},{'[',2},{']',3},{')',4}};  //定义括号匹配 
        bool st=1;
        int n=s.size();
        for(int i=0;i<n;i++)
        {
            if(h[s[i]]<=2) //左括号直接入栈 
            {
                stk.push(s[i]);
            }
            else //右括号 
            {
                if(stk.size()&&h[stk.top()]+h[s[i]]==5) //有元素且匹配 
                {
                    stk.pop(); //删除当前栈定元素,继续判断下个字符 
                    continue;
                }
                else //不匹配 
                {
                    st=false;
                }
            }
        }
        if(stk.size()) st=false; //整个字符匹配完了栈中还有元素 
        if(st==true) cout<<"OK";
        else cout<<"Wrong";
        return 0;
    }
    
    • 1
      @ 2024-8-21 11:10:56
      #include <bits/stdc++.h>
      using namespace std;
      const int N=300;
      char a[N];
      stack<int>k;
      int main()
      {
      	bool fx=true;
      	int i;
      	int s_1=0,b_1=0,s_2=0,b_2=0; 
      	cin>>a;
      	i=strlen(a);
      	for(int j=0;j<i;j++)
      	{
      		
      		if(a[j]=='[')
      		{
      			s_2++;
      			k.push(1);
      			fx=true;
      			continue;
      		}
      		if(a[j]==']')
      		{
      			b_2++;
      			if(b_2>s_2)
      			{
      				fx=true;
      				break;
      			}
      			if(k.top()==1)
      			{
      				fx=false;
      				k.pop();
      			} 
      			else break;
      			if(s_2==b_2)fx=false;
      			continue;
      		}
      		if(a[j]=='(')
      		{
      			s_1++;
      			k.push(2);
      			continue;
      		}
      		if(a[j]==')')
      		{
      			b_1++;
      			if(b_1>s_1)
      			{
      				fx=true;
      				break; 
      			} 
      			if(k.top()==2)
      			{
      				fx=false;
      				k.pop();
      			} 
      			else break;
      			if(s_1==b_1)fx=false;
      			continue;
      		}	
      	}
      	if(s_2>b_2 || s_1>b_1)
      	{
      		fx=true;
      	}
      	if(fx==true)cout<<"Wrong";
      	if(fx==false) cout<<"OK"; 
      	return 0;
      }
      
      • 0
        @ 2025-9-26 20:08:26
        #include<bits/stdc++.h>
        using namespace std;
        stack<char> stk;
        int main()
        {
        	string s;
        	cin>>s;
        	int n=s.size();
        	bool st=true; 
        	for(int i=0;i<n;i++)
        	{
        		if(s[i]=='['||s[i]=='(')
        		{
        			stk.push(s[i]);
        		}
        		else
        		{
        			if(stk.size()&&(stk.top()=='['&&s[i]==']'||stk.top()=='('&&s[i]==')'))
        			{
        				stk.pop();	
        			}	
        			else
        			{
        				st=false;
        			}
        		}
        	}
        	if(stk.size()) st=false;
        	if(st) cout<<"OK";
        	else cout<<"Wrong"; 
        	return 0;
        }
        
        
        • 1

        Information

        ID
        967
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        3
        Tags
        (None)
        # Submissions
        72
        Accepted
        17
        Uploaded By