Sunday, May 1, 2016

URI Online Judge Solution 1068 Parenthesis Balance I

Main link : https://www.urionlinejudge.com.br/judge/en/problems/view/1068


URI Online Judge | 1068

Parenthesis Balance I

By Neilor Tonin, URI  Brazil
Timelimit: 1
Considering an expression with parenthesis, print a message informing if the among of parenthesis is correct or incorrect, without considering the rest of the expression. Example:

a+(b*c)-2-a        is correct
(a+b*(2-c)-2+a)*2  is correct
when
(a*b-(2+c)         is incorrect
2*(3-a))           is incorrect
)3+b*(2-c)(        is incorrect
Resuming, all closing parenthesis must have an open parenthesis and it's not possible a closing parenthesis without a previous open parenthesis, and the quantity of closing and open parenthesis must be the same.

Input

The input file contains expressions (1 <= <= 10000), each one with up to 1000 characters. 

Output

The output must be correct or incorrect for each test case according with above rules.
Input SampleOutput Sample
a+(b*c)-2-a
(a+b*(2-c)-2+a)*2
(a*b-(2+c)
2*(3-a))
)3+b*(2-c)( 
correct
correct
incorrect
incorrect
incorrect

Solution in C++ language:

#include <iostream>
#include <cstring>
#include <stack>
using namespace std;

int main()
{
 int size;
 string line;

 while(getline(cin, line))
 {
  size = line.length();
  stack<char> s;

  for (int i = 0; i < size; ++i)
  {
   if(line[i] == '(')
    s.push(i);
   if(line[i] == ')'){
    if(!s.empty()){
     s.pop();
    }else{
     s.push(i);
    }
   }

  }

  if(s.empty()){
   cout << "correct" << endl;
  }else{
   cout << "incorrect" << endl;
  }
 }

 return 0;
}

Description of URI 1068 problem:

This is a simple problem of stack. If you haven't enough idea what is stack, don't touch it before learning about stack. To learn about stack you can visit this site :
http://funny-coder.blogspot.com/2016/04/a-details-on-stack-using-c-language.html
In here, you can learn the basic of stack data structures. You need to know well what is push and pop method. How stack work.

So, now come to the point In this code we have to check if the given line has perfect match parenthesis. First we push the  " (  " in the stack and then if we get "  ) " then we take two action :

  1. If stack is not empty then pop previous data 
  2. otherwise push it to the stack
By this way finally we will get the solution
  1. If stack has no parenthesis, that means our given value is correct , otherwise our given value is not correct.


Note:
 C++ and java has special stack class. But C has not. If you first code with C then obviously you can do it by C++ or Java easily. So go to

http://funny-coder.blogspot.com/2016/04/a-details-on-stack-using-c-language.html

No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.