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

Re: 673 - Parentheses Balance

Postby vinocit » Mon Oct 13, 2008 10:23 am

Hai guys. I dont reply to any post but do keep in mind that empty string has to give out "Yes" . I did this mistake and was looking into it for 3 days
vinocit
New poster
 
Posts: 10
Joined: Mon Oct 13, 2008 10:11 am

Re: 673 - WA Please Help

Postby anton_indrawan » Mon Dec 15, 2008 4:04 pm

Help me, i don't know why i got WA, i already test my code with many input...
Code: Select all
 got AC .. code removed
Last edited by anton_indrawan on Tue Dec 16, 2008 5:21 pm, edited 1 time in total.
anton_indrawan
New poster
 
Posts: 6
Joined: Mon Dec 15, 2008 3:34 pm
Location: Surabaya, Indonesia

Re: 673 - Parentheses Balance

Postby mf » Mon Dec 15, 2008 4:11 pm

Here are a couple of examples where your program goes wrong:
Code: Select all
2
[]
(([())])


Your algorithm is too complex for such a simple problem. You only need a simple stack to solve it.
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

Re: 673 - Parentheses Balance

Postby anton_indrawan » Tue Dec 16, 2008 5:19 pm

Hai mf, thank about your information..
I got AC with 0.050 seconds using the stack (include<stack>)

Is it necessary to create own class stack ?
Which one is faster, own class stack or using the lib one ?

Thx Before.. Sorry for my language
anton_indrawan
New poster
 
Posts: 6
Joined: Mon Dec 15, 2008 3:34 pm
Location: Surabaya, Indonesia

Re: 673 - Parentheses Balance

Postby mf » Wed Dec 17, 2008 10:15 am

Is it necessary to create own class stack ?

No. And why do you even ask? If you feel like writing your own stack, just do it, it's very simple:
Code: Select all
int stk[1000], top = 0;  // the stack

push an element x:  stk[top++] = x;
popping an element: stk[--top]

You see, the ops are so simple, you don't even need a class to hold them.
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

Re: 673 - Parentheses Balance

Postby poka » Wed Dec 17, 2008 2:34 pm

Could anyone help me, i don't know why i got WA. Here is my code in java.


import java.io.*;
import java.util.*;

public class Main
{
public static boolean oklepaji(String niz)
{
Stack<Character> s = new Stack<Character>();
for (int i = 0; i < niz.length(); i++)
{
char znak = niz.charAt(i);
if (znak == '(' || znak == '[') s.push(znak);
else if (znak == ')' && !s.empty() && s.peek() == '(') s.pop();
else if (znak == ']' && !s.empty() && s.peek() == '[') s.pop();
else return false;
}
if (!s.empty()) return false;
return true;
}

public static void main(String[] args) throws IOException
{
BufferedReader vhod = new BufferedReader(new
InputStreamReader(System.in));

int n = Integer.parseInt(vhod.readLine());
String[] tab = new String[n];
for (int i = 0; i < n; i++)
{
tab[i] = vhod.readLine();
}
System.out.println();
for (int i = 0; i < n; i++)
{
if (oklepaji(tab[i])) System.out.println("Yes");
else System.out.println("No");
}
}
}
poka
New poster
 
Posts: 2
Joined: Wed Dec 17, 2008 2:29 pm

Re: 673 - Parentheses Balance

Postby mf » Wed Dec 17, 2008 2:39 pm

Probably because you have an unnecessary System.out.println();
mf
Guru
 
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

Re: 673 (dont open a new thread.......just search the prob. num.

Postby mars kaseijin » Thu Dec 18, 2008 4:19 am

I went through problem description looking for sort and ordered in addition to balanced output. I found none, hence "([)]" should be Yes. Meaning the input can be jumbled as long as it balances in the end.

kbr_iut wrote:dont open a new thread as there is already many threads...think about that.
however ur program gives wrong output for this set of input.
input:
Code: Select all
7
([])
(([()])))
([()[]()])()
([)]
(
()(
[()

ur output is:
Code: Select all
Yes
No
Yes
Yes
No
No
No


My AC program gives:
Code: Select all
Yes
No
Yes
No
No
No
No


Hope it will help.
mars kaseijin
New poster
 
Posts: 19
Joined: Mon Sep 19, 2005 4:58 am

Re: 673 - Parentheses Balance

Postby anton_indrawan » Thu Dec 18, 2008 1:21 pm

Hay mf
thx for your information, you are very experienced ... ^__^
anton_indrawan
New poster
 
Posts: 6
Joined: Mon Dec 15, 2008 3:34 pm
Location: Surabaya, Indonesia

Re: 673 - Parentheses Balance

Postby poka » Fri Dec 19, 2008 1:48 pm

Thanks, my program was accepted, just one system.out.println() was wrong....
poka
New poster
 
Posts: 2
Joined: Wed Dec 17, 2008 2:29 pm

Re: 673 - Parentheses Balance

Postby abid_iut » Tue Dec 23, 2008 7:15 pm

please help
I am getting WA but why
it passed all the input in the boards
here is the code:
Code: Select all
Removed

pls help :cry:
Last edited by abid_iut on Thu Dec 25, 2008 7:39 am, edited 1 time in total.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/
abid_iut
Learning poster
 
Posts: 80
Joined: Wed Jul 16, 2008 7:34 am

Re: 673 - Parentheses Balance

Postby Obaida » Wed Dec 24, 2008 8:48 am

See my PM. You will get a good solution. :)
try_try_try_try_&&&_try@try.com
This may be the address of success.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 673 - Parentheses Balance

Postby abid_iut » Wed Dec 24, 2008 10:11 am

Thanks for ur PM
I am trying in that way. BUT CAN U PLS CHECK MY GIVEN CODE AND TELL ME IS THERE ANY MISTAKE?
and about ur logic in pm
can u pls explain input according to ur PM :

([)]

it should be no.
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/
abid_iut
Learning poster
 
Posts: 80
Joined: Wed Jul 16, 2008 7:34 am

Re: 673 - Parentheses Balance

Postby Obaida » Wed Dec 24, 2008 11:10 am

This is a special case,
([)]

here when you get any input "[" then you make a Boolean variable 1 and then if you get any ")" then break and no. But if you get "]" before ")" then make the Boolean variable to 0.

Is it clear??? :wink:
try_try_try_try_&&&_try@try.com
This may be the address of success.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 673 - Parentheses Balance

Postby abid_iut » Thu Dec 25, 2008 7:41 am

Now what is the problem?
It is giving WA but i think It passes all input
here is the code:
Code: Select all
#include<stdio.h>
#include<string.h>

char fb[200],tb[200];

int main()
{
   char line[200]="";
   int i,j,n,k,flag,l,p,q,an;
   scanf("%d",&n);
   for(int it=0;it<n;it++){
      gets(line);
      if(line[0]==NULL){printf("Yes\n");continue;}
      k=0;l=0;flag=0;an=0;
      for(i=0;line[i];i++){
         if(line[i]==')' && fb[0]==NULL){flag=1;break;}
         if(line[i]==']' && tb[0]==NULL){flag=1;break;}
         if(line[i]=='(' && line[i+1]==']'){flag=1;break;}
         if(line[i]=='[' && line[i+1]==')'){flag=1;break;}
         if(line[i]=='('){
            fb[k]='(';k++;
            p=k;
         }
         if(line[i]=='['){
            tb[l]='[';l++;
            q=l;
         }
         if(line[i]==')'){
            fb[p-1]=NULL;
            p--;
            k=p;
         }
         if(line[i]==']'){
            tb[q-1]=NULL;
            q--;
            l=q;
         }
      }
      if(flag==1){printf("No\n");}
      else {
         for(i=0;line[i];i++){
            if(fb[i]==NULL && tb[i]==NULL)an=1;
            else {
               an=0;break;
            }
         }
         if(an==1)printf("Yes\n");
         else printf("No\n");
         for(i=0;line[i];i++)line[i]=NULL;
      }
   }
   return 0;
}

pls help :(
i love to wait... wait for better... and better will come...
http://akanoi.webs.com/
abid_iut
Learning poster
 
Posts: 80
Joined: Wed Jul 16, 2008 7:34 am

PreviousNext

Return to Volume VI

Who is online

Users browsing this forum: No registered users and 1 guest