Page 8 of 17

Posted: Sun Feb 25, 2007 4:57 pm
by asif_rahman0
Try the following input:

Code: Select all

2
([]))
Then see, what will happen?
bye

Posted: Tue Feb 27, 2007 4:46 pm
by nameofevil
i see, :) Thx

Posted: Mon Mar 19, 2007 12:59 pm
by jainal cse du
Can Anybody Tell me Why I am getting RE?

Code: Select all

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
       char str[150];

       int top = 0,i,n;
       char stack[150];
       scanf("%d%*c",&n);
       while(n--)
       {

	       gets(str);
	       int left = 0;
	       int right = 0;
	       for(i = 0; str[i]; i++)
	       {

		       if( (str[i] == '(') || (str[i] == '[') )
		       {        stack[top++] = str[i];
				left++;
		       }

		       if( (str[i] == ')') )
		       {
			       if( stack[--top] == '(' )
					right++;
			       else
					break;

		       }
		       if( (str[i] == ']') )
		       {
			       if(stack[--top] == '[')
					right++;
			       else
					break;
		       }
	       }
	       if(left == right)
		       printf("Yes\n");
	       else
			printf("No\n");
       }
       return 0;
}
Advance Thanks

Posted: Mon Mar 19, 2007 1:30 pm
by shamim
You have to set top=0 before each case.
This correction will give you wrong answer.

To jainal cse du ...

Posted: Mon Mar 19, 2007 7:18 pm
by nymo
To jainal cse du,
I just skim through your code, not going thorougly ...
however, your code breaks when a mismatching end bracket occurs... think about a test case when upto the mismatching end bracket (left == right) ;) ; your code will output Yes though it shouldn't.

Posted: Sat May 12, 2007 4:59 pm
by hamedv
what's wrong with my code?


#include <stdio.h>

int i, st, stack[129], t, l;
bool b;
char s[129];

int main()
{
scanf("%d", &t);
for (l = 0; l < t; l++)
{
scanf("%s", &s);
st = 0;
b = 1;
for (i = 0; s != 0; i++)
{
if (s == '(')
{
st++;
stack[st] = s;
} else
if (s == '[')
{
st++;
stack[st] = s;
} else
if (s == ')')
{
if (stack[st] != '(') b = 0;
st--;
} else
if (s == ']')
{
if (stack[st] != '[') b = 0;
st--;
}
if (st < 0) b = 0;
}
if (st != 0) b = 0;
if (b)
puts("Yes");
else
puts("No");
}
}
[/code]

Posted: Thu Jul 19, 2007 5:09 pm
by spider_6765
try this input set. May be its with your algorithm...
your code generates wrong output for these inputs.
for everycase in the following input set your code should print No
4
(])
[)]
[)(]
[))((]
(]][[)

Posted: Thu Jul 19, 2007 5:20 pm
by spider_6765
using bool as a variable will result in compile error.
change 'bool' into 'boool' or 'booool' or anything else. :lol:

n e way.. you will get a WA once you correct this problem. check your algo to solve it.

and remember... DONOT use int, long, double etc as variable NAMES because these are variable TYPES;

Posted: Thu Jul 19, 2007 5:27 pm
by spider_6765
your code doesn't show the right outputs for this inputs

Code: Select all

4
(])
[)]
[)))]
[[)]

Thx

Posted: Mon Jan 21, 2008 9:57 pm
by thomas1016
Thanks a lot .
A blank line is truly a critical input !!!!

:P :P :P

wa

Posted: Tue Jan 22, 2008 6:34 am
by Mata
hi, I try the input in the forum, but I got wa, here is my code, what could be wrong my program?.

Code: Select all

got Ac

673 wa

Posted: Wed May 07, 2008 12:03 pm
by mukit_sust
ac

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

Posted: Wed May 07, 2008 12:46 pm
by kbr_iut
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.

Re: 673 - Parentheses Balance

Posted: Fri May 30, 2008 5:53 pm
by vivgrn
dear all
I have been troubled for the past few weeks solving this problem and have applied all tricks that have been given in the forum.
Yet I have been unable to get an AC.
My code is as follows

Code: Select all

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

int main()
{

int n;
cin>>n;
fflush(stdin);
while(n>0)
{
          char str[129];
          stack<char> s;
          gets(str);
         
          
          if(strlen(str)!=0)
          {
          int i=0;
          bool flag=true;
          while(flag==true && i<strlen(str))
          {
                           if(str[i]=='('||str[i]=='[')
                           {s.push(str[i]);i++;}
                           else 
                           {
                                          if(str[i]==']')
                                          {
                                                       if(!s.empty())
                                                       {
                                                         if(s.top()=='[')
                                                         {
                                                         s.pop();
                                                         i++;
                                                         }
                                                         else
                                                         {
                                                             flag==false;
                                                             break;
                                                         }
                                                       }
                                                       else
                                                       {
                                                       flag=false;
                                                       break;
                                                       }
                                          }
                                          if(str[i]==')')
                                          {
                                                         if(!s.empty())
                                                         {
                                                         if(s.top()=='(')
                                                         {
                                                         s.pop();
                                                         i++;
                                                         }
                                                         else
                                                         {
                                                             flag==false;
                                                             break;
                                                         }
                                                         }
                                                         else
                                                         {
                                                             flag=false;
                                                             break;
                                                         }
                                          }
                           }
          }      
          if(s.empty()&&i==strlen(str))
          cout<<"Yes"<<endl;
          else
          cout<<"No"<<endl; 
          }
          else
          cout<<"Yes"<<endl;                                          
          n--;
}


return 0;
}
I would request you to kindly go through the code and tell me where does the problem lie.
Thanking you in advance.

Re: 673 - Parentheses Balance

Posted: Sun Jun 29, 2008 9:28 pm
by Samudra Banerjee
Always getting a Wrong Answer for this code.....I have tried all possible test cases..Can anyone please help ?

Code: Select all

#include<stdio.h>
#include<string.h>
int main()
{
    char c,s[129],stack[129];
    int sp=-1,i,j,n,l,k;
    scanf("%d",&n);
    c=getchar();
    for(i=1;i<=n;i++)
    {
     sp=-1;k=0;
     while(1)
     {
      c=getchar();
      s[k++]=c;
      if(c=='\n' || c==EOF) {s[k]='\0';break;}
     }
     l=strlen(s);
     for(j=0;j<l;j++)
     {c=s[j];
     if(c=='[' || c=='(') stack[++sp]=c;
     else if(c==')') if(sp>=0 && stack[sp]=='(') sp--;
                     else break;
     else if(c==']') if(sp>=0 && stack[sp]=='[') sp--;
                     else break;
     }
     if(j==l && sp==-1) printf("Yes\n"); else printf("No\n");
    }
    return 0;
}