Solution of 551 - Nesting a Bunch of Brackets

Problem Description
source:https://uva.onlinejudge.org/external/5/551.html

In this problem we consider expressions containing brackets that are properly nested. These expressions are obtained by juxtaposition of properly netsted expressions in a pair of matching brackets, the left one an opening and the right one a closing bracket.
        ( a + $ ( b = ) ( a ) ) is properly nested 
        ( a + $ ) b = ) ( a ( ) is not 

In this problem we have several pairs of brackets, so we have to impose a second condition on the expression: the matching brackets should be of the same kind. Consequently ‘(())’ is OK, but ‘([))’ is not. The pairs of brackets are:

(   ) 
[   ] 
{   } 
<   > 
(*   *)  

    The two characters ‘(*’ should be interpreted as one symbol, not as an opening bracket ‘(’ followed immediately by an asterisk, and similarly for ‘*)’. The combination ‘(*)’ should be interpreted as ‘(*’ followed by ‘)'.
    Write a program that checks wheter expressions are properly nested. If the expression is not properly nested your program should determine the position of the offending bracket, that is the length of the shortest prefix of the expression that can not be extended to a properly nested expression. Don’t forget ‘(*’ counts as one, as does ‘*)’. The characters that are not brackets also count as one.

Input 

The input is a text-file. Each line contains an expression to be checked followed by and end-of-line marker. No line contains more than 3000 characters. The input ends with a standard end-of-file marker. 

Output 

The output is a textfile. Each line contains the result of the check of the corresponding inputline, that is ‘YES’ (in upper case), if the expression is OK, and (if it is not OK) ‘NO’ followed by a space and the position of the error. 

Sample Input 

(*a++(*)
(*a{+}*)

Sample Output 

NO 6 
YES

Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()
{
    long int i, count,len;
    char expression[3055],ch;
    while(gets(expression))
    {
        stack<char> mystack;
        len=strlen(expression);
            if(len==0)
            {
                printf("Yes\n");
                continue;
            }
        count=0;
        for(i=0;i<len;i++)
        {
           if(expression[i]=='('||expression[i]=='{'||expression[i]=='['||expression[i]=='<'||(expression[i-1]=='('&&expression[i]=='*'))
            {
                mystack.push(expression[i]);
                if(expression[i]!='*')
                    count+=1;
            }
            else if((expression[i]==')'||expression[i]=='}'||expression[i]==']'||expression[i]=='>'||(expression[i]=='*'&&expression[i+1]==')')))
            {
                if(mystack.empty() &&expression[i]=='*')
                {
                    count+=1;
                    break;
                }
                else if(mystack.empty() &&(expression[i]==')'||expression[i]=='}'||expression[i]==']'||expression[i]=='>'))
                {
                    count+=1;
                    break;
                }
                else if(mystack.top()=='*'&&expression[i]=='*' )
                {
                    mystack.pop();
                    mystack.pop();
                    count+=1;
                    i+=1;
                }

                else if(!mystack.empty() &&((mystack.top()=='('&&expression[i]==')')||(mystack.top()=='{'&&expression[i]=='}')||(mystack.top()=='['&&expression[i]==']')||(mystack.top()=='<'&&expression[i]=='>')))
                {
                    mystack.pop();
                    count+=1;
                }
                else
                {
                    count+=1;
                    break;
                }
            }
            else
                count+=1;
        }
        if(mystack.empty() && len==i)
            printf("YES\n");
        else if(!mystack.empty() && len==i)
            printf("NO %ld\n",count+1);
        else
            printf("NO %ld\n",count);
    }
    return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience