1 solutions

  • 0
    @ 2024-12-10 22:16:31

    C :

    #include <stdio.h>
    main() {
        char s[256],d[256];int i,j=0;
    	scanf("%s",s);
    	for(i=0;s[i]!='\0';i++){
    		if(s[i]=='(' ||s[i]=='['){
    			d[j]=s[i];j++;
    		}
    		if(s[i]==')'){
    			if(d[j-1]=='(')j--;
    				else {j--;break;}
    		}
    		if(s[i]==']'){
    			if(d[j-1]=='[')j--;
    				else {j--;break;}
    		}		
    	}
        if (j==0) printf("OK");
        	else printf("Wrong");	
    } 
    

    C++ :

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    string s;
    bool my_judge(string);
    int main()
    {
    	//freopen("check10.in","r",stdin);
    	//freopen("check10.out","w",stdout);
    	cin>>s;
    	if(my_judge(s))cout<<"OK"<<endl;
    	else cout<<"Wrong"<<endl;
    	return 0;
    }
    bool my_judge(string s)
    {
    	int top=0,i=0;
    	char a[250];
    	while(i<s.size())
    	{
    		if(s[i]=='(')
    		{
    			top++;
    			a[top]='(';
    		}
    		if(s[i]=='[')
    		{
    			top++;
    			a[top]='[';
    		}
    		if(s[i]==')')
    		{
    			if(top==0)return false;
    			if(a[top]=='(') top--;
    			else return false;
    		}
    		if(s[i]==']')
    		{
    			if(top==0)return false;
    			if(a[top]=='[') top--;
    			else return false;
    		}
    		i++;
    	}
    	if(top==0)return true;
    }
    
    • 1

    Information

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