1 solutions

  • 0
    @ 2025-11-5 17:20:03

    C :

    #include <stdio.h>
    #include <string.h>
    #define MAX 26
    
    
    int main()
    {
    	char _stack[MAX];
    	int top;
    	char a[30];
    	while (scanf("%s", &a) != EOF)
    	{
    		top = -1;
    		for (int i = 0; i<strlen(a); i++)
    		{
    			//栈非空
    			if (top != -1)
    			{
    				//出栈
    
    				if (_stack[top] == '('&&a[i] == ')' || _stack[top] == '['&&a[i] == ']')
    					top--;
    				else{
    					_stack[++top] = a[i];
    				}
    			}
    			else{
    				_stack[++top] = a[i];
    			}
    		}
    		if (top == -1)
    		{
    			printf("Yes\n");
    		}
    		else
    			printf("No\n");
    	}
    
    	return 0;
    
    
    }
    
    

    C++ :

    #include <stdio.h>
    #include <stack>
    #include <string.h>
    using namespace std;
    int main()
    {
    	int len,i,check;
    	char a[50],c;
    	while(gets(a))
    	{
    		stack<char > que;
    		que.push('b');
    		check=1;
    		len=strlen(a);
    		for(i=0;i<len;i++)
    		{
    			if(a[i]=='('||a[i]=='[')
    			{
    			que.push(a[i]);
    			continue;
    		}
    			else
    			if(a[i]==')'||a[i]==']')
    			{
    				if(a[i]==')')
    				{
    					if(que.top()=='(')
    					{
    						que.pop();
    						continue;
    					}
    					else
    					{
    						check=0;
    						break;
    					}
    				}
    				if(a[i]==']')
    				{
    					if(que.top()=='[')
    					{
    						que.pop();
    						continue;
    					}
    					else
    					{
    						check=0;
    						break;
    					}
    				}
    			}
    		}
    		if(check&&que.size()==1)
    		printf("Yes\n");
    		else
    		printf("No\n");
    	}
    	return 0;
    }
    
    • 1

    Information

    ID
    18310
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By