673 - Parentheses Balance

All about problems in Volume VI. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Using STL

Postby jan_holmes » Wed Nov 16, 2005 9:25 pm

Yeah... using STL is possible,but when I submitted my code using STL,I got a very2 slow AC..... Maybe using Bruteforce could reduce the runtime... :) Hope it helps...
jan_holmes
Experienced poster
 
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore

Postby Timo » Thu Nov 17, 2005 12:59 pm

I think this problem can be solved easily if you use stack.

- scan the string from left to right
- if find '(' or '[' push them into your stack
- if find ')' or ']' and the top of the stack is not they match then break from
the looping the answer is "No". Otherwise pop from the stack
- after you finished scan the string but the stack is not empty then the answer
is "No", otherwise "Yes".

I hope you understand what I mean and can get AC.

:D :D :D :D :D
Timo
Learning poster
 
Posts: 70
Joined: Tue Oct 11, 2005 2:44 am
Location: Indonesia

Postby Roby » Wed Nov 23, 2005 1:46 pm

Here are some input for your program:
Code: Select all
4
([])
(([()])))
([()[]()])()
[(])

The output should be:
Code: Select all
Yes
No
Yes
No
Last edited by Roby on Sun Nov 27, 2005 5:04 pm, edited 1 time in total.
User avatar
Roby
Experienced poster
 
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia

Postby jjtse » Sat Nov 26, 2005 5:34 pm

I don't think '{' and '}' can be a valid input character.

INPUT:
The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.
jjtse
Learning poster
 
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US

Postby jjtse » Sat Nov 26, 2005 6:00 pm

can there be spaces between characters in the input string?


i.e. : "([]) []"
jjtse
Learning poster
 
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US

Postby ayon » Sun Nov 27, 2005 9:44 am

no, there cant be any spaces between the characters. bcos it is said that the string consisting of parentheses () and []. surely there wont be any '{' or '}'
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
ayon
Experienced poster
 
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Postby Roby » Sun Nov 27, 2005 4:59 pm

Yes, the input wouldn't have '{' and '}'. :oops: I've mistaken for read the problem description (lucky me, my code give me AC). And there also wouldn't any spaces given in the input.
Code: Select all
The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.
Sorry... :wink:
User avatar
Roby
Experienced poster
 
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia

673 .... WA , please help me

Postby Wei-Ming Chen » Thu Dec 29, 2005 2:31 pm

What's wrong?

#include <stdio.h>
#include <string.h>
int main()
{
int a,b,e,f,g,h;
Last edited by Wei-Ming Chen on Tue Jan 03, 2006 1:38 pm, edited 1 time in total.
Wei-Ming Chen
Experienced poster
 
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

Postby ayon » Fri Dec 30, 2005 3:29 pm

for input
)(
output must be "No"
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
ayon
Experienced poster
 
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Postby Wei-Ming Chen » Tue Jan 03, 2006 1:41 pm

I made a very very silly mistake.....

I wanted to write "&&" , but wrote "||" instead.....

Thanks Ayon, you helped me solve this problem.
Wei-Ming Chen
Experienced poster
 
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

673 How use cin in C++

Postby fayyazkl » Wed Jun 21, 2006 2:18 pm

It seems quite silly to answer a post two years old. But i thought just in case.... :)

HERE IS THE POST

Zhao Le wrote:

I have occurred to the same question.

I can solve when using
first gets();
then atoi();

but it is C like.

I hate to both include iostream and stdio
my question is how can I turn it to C++ iostream when occurred to the multiply input.
_________________
AC makes me feels good,
But WA makes me thinks hard.

MY RESPONSE:

# include <iostream>

int n;
char str[150];

cin >> n;
cin.ignore(); // this ignores the \n at the end of line
cin >> str; // or you might use cin.getline(str,130) as an alternate
There are 10 types of people. Those who can read binary and those who cant ;)
http://acm.uva.es/problemset/usersjudge.php?user=37504
fayyazkl
New poster
 
Posts: 11
Joined: Thu Mar 16, 2006 8:02 am
Location: Lahore, Pakistan

673 , RE why??

Postby alfreadx » Sun Aug 13, 2006 9:38 am

Code: Select all

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

stack<char> x;
bool judge(char *in)
{
   static int i,len;

   while(!x.empty())   x.pop();
   
   len=strlen(in);
   if(in[0] == ']' || in[0] == ')')   return 0;
   if(len % 2 != 0)   return 0;
   for(i=0;i<len;++i){
      switch(in[i]){
         case '[':
         case '(':   x.push(in[i]);   break;

         case ']':   if(x.top() == '[')   x.pop();
                  else    return 0;
                  break;
         case ')':   if(x.top() == '(')   x.pop();
                  else    return 0;
                  break;
         default : break;
      }

   }
   if(x.empty())   return 1;
   else return 0;

}
int main()
{
   int j,times;
   char in[130];

   times=atol(gets(in));
   for(j=0;j<times;++j){
      gets(in);

      if(judge(in))   cout << "Yes\n";
      else cout << "No\n";


   }


    system("pause");
    return 0;
}


i submit code , but i got RE , why ??
can you help me find bug .
thank you.
alfreadx
New poster
 
Posts: 4
Joined: Fri Aug 04, 2006 11:06 am
Location: Taiwan

Postby jan_holmes » Sun Aug 13, 2006 11:02 am

remove "system("pause")" from your code.
jan_holmes
Experienced poster
 
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore

oh! thank you!!

Postby alfreadx » Sun Aug 13, 2006 11:52 am

oh ! thank you !!

but i got RE .

thanks again.

who can help me find bug in my code .

thank you.
alfreadx
New poster
 
Posts: 4
Joined: Fri Aug 04, 2006 11:06 am
Location: Taiwan

Postby jan_holmes » Sun Aug 13, 2006 7:02 pm

try this input :

1
(()))(


The answer should be "No".
jan_holmes
Experienced poster
 
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore

PreviousNext

Return to Volume VI

Who is online

Users browsing this forum: No registered users and 0 guests

cron