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

673 WA

Postby LMG » Sun Aug 20, 2006 11:49 pm

I tried to use stack to implement the work and my code got correct answers always. I don't know where is the bug. Can somebody help me?

#include <stdio.h>
#include <string.h>
main()
{
char stack[130];
char c[130];
char s[130];
int n;
int i, j;
int len;
scanf("%d\n", &n);
for(i=0; i<130; i++)
{
stack[i]='\0';
s[i]='\0';
c[i]='\0';
}
while(n>0)
{
gets(s);
len=strlen(s);
for(i=0; i<len; i++)
c[i]=s[i];
j=0;
for(i=0; i<len; i++)
{
if(c[i]=='(')
{
stack[j]=c[i];
j++;
}
else if(c[i]=='[')
{
stack[j]=c[i];
j++;
}
else if(c[i]==')')
{
if(stack[j-1]=='(')
{
j--;
stack[j]='\0';
}
else
{
j--;
break;
}
}
else if(c[i]==']')
{
if(stack[j-1]=='[')
{
j--;
stack[j]='\0';
}
else
{
j--;
break;
}
}
}
if(j==0)
printf("Yes\n");
else
printf("No\n");
n--;
}
}
LMG
New poster
 
Posts: 2
Joined: Sun Aug 20, 2006 11:35 pm

Postby felipealmeida » Tue Oct 03, 2006 2:51 pm

i also got WA and i can't see any mistake :cry:

ps: i wrote it in portuguese, so
pilha = stack
topo = top
vet = array
entrada = input

[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 128

#define PAR 0
#define COL 1

typedef struct _pilha
{
int vet[MAX];
int topo;
} pilha;

void inipilha(pilha *p)
{
p->topo = -1;
}

int topo(pilha p)
{
return p.vet[p.topo];
}

void push(pilha *p, int valor)
{
(p->topo)++;
p->vet[p->topo] = valor;
}

int pop(pilha *p)
{
int valor = p->vet[p->topo];
(p->topo)--;

return valor;
}

int main(void)
{
pilha p;

char entrada[MAX];

char sqtd[MAX];
int qtd;

int i;
int j;


fgets(sqtd, MAX, stdin);
qtd = atoi(sqtd);

for (i = 0; i < qtd; i++)
{
inipilha(&p);
fgets(entrada, MAX, stdin);
entrada[strlen(entrada)-1] = '\0';

for (j = 0; j < strlen(entrada); j++)
{
if (entrada[j] == '(')
{
push(&p, PAR);
}
else if (entrada[j] == '[')
{
push(&p, COL);
}
else if (entrada[j] == ')' && p.topo >= 0 && topo(p) == PAR)
{
pop(&p);
}
else if (entrada[j] == ']' && p.topo >= 0 && topo(p) == COL)
{
pop(&p);
}
else
{
p.topo = -2; /* um numero qualquer para dizer que t
felipealmeida
New poster
 
Posts: 3
Joined: Tue Oct 03, 2006 2:49 pm

673 - Parentheses Balance

Postby felipealmeida » Thu Nov 09, 2006 3:32 pm

I always get WA when I submit, but I can't see any mistake.
I've tried many different inputs and all of them I've got correct answer.

my code:
Code: Select all
* edited *
Last edited by felipealmeida on Tue Nov 14, 2006 6:45 pm, edited 2 times in total.
felipealmeida
New poster
 
Posts: 3
Joined: Tue Oct 03, 2006 2:49 pm

Postby seulkiro » Fri Nov 10, 2006 9:38 pm

Try the following input:

Code: Select all
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))


64 open brackets and 64 close brackets.

I think the answer should be Yes, since the maximum string length is 128 according to the description.
waikiki..
User avatar
seulkiro
New poster
 
Posts: 6
Joined: Sun Feb 15, 2004 2:13 am
Location: Canada

Postby felipealmeida » Tue Nov 14, 2006 6:46 pm

thank you very much!

string length wasn't large enough...


:D
Undergraduating in Computer Science
Federal University of Rio de Janeiro
felipealmeida
New poster
 
Posts: 3
Joined: Tue Oct 03, 2006 2:49 pm

673

Postby uvapotta » Fri Dec 08, 2006 12:24 am

I got Run time error in my code but test but it pass all test that i have done
Can any one help me?

Code: Select all
#define DIM 129
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
   int i=0,col=0,j,n,par=0;
   char **string,*e;
   
   scanf("%d",&n);
   gets(e);
   string = (char **) malloc (n * sizeof(char *));    
   for (i=0; i<DIM; i++)
     {
        *(string +i) = (char *) malloc(DIM * sizeof (char));
     }
     
   for(j=0;j<n;j++){
     gets(string[j]);
    }

   for(j=0;j<n;j++)
   {      
      par=0; col=0;
   
      {
      for(i=0;i<DIM;i++)
      {
               
         if( string[j][i] == '(')
         {
            par++;
         }
         
         if( string[j][i] == ')')
         {
            par--;
         }
         
         if( string[j][i] == '[')
         {
            col++;
         }
         
         if( string[j][i] == ']')
         {
            col--;
         }
         if((col <0) || (par <0) || (string[j][i] == '\0') )
         {
         break;
         }
         
                      
   
       }
       }
   if((par == 0) && (col == 0))
      puts("Yes");
   
   else puts("No");
   }
   
   
   
   
      
   return 0;
}
[/code]
uvapotta
New poster
 
Posts: 2
Joined: Fri Dec 08, 2006 12:21 am

Postby Jan » Fri Dec 08, 2006 5:07 am

Search your problem in the board first. If you find a thread then try to post in that thread. Dont open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby uvapotta » Fri Dec 08, 2006 5:41 am

Ok Sorry I will post there
uvapotta
New poster
 
Posts: 2
Joined: Fri Dec 08, 2006 12:21 am

Postby nameofevil » Sun Feb 25, 2007 4:08 pm

I also got WA with my code, and it seem correct in all sample inputs I fund in this forum. plz help :(

Code: Select all
#include<stdio.h>
#include<string.h>
int main(){
   int N,index,stop,string[130];
   bool res;
   char str[130] ;
   if(fgets(str,129,stdin) == NULL){
      return 1;
   }
   sscanf(str,"%d\n",&N);
   while(N--){
      index = stop = 0;
      res = true;
      for( ;  res && stop == 0 ; ){
         switch(getchar()){
            case '\n':
               stop = 1;
               break;
            case '(':
               string[index++] = 1;
               break;
            case '[':
               string[index++] = 2;
               break;
            case ')':
               if(index == 0 || (index>0 && string[--index] != 1) ){
                  res = false;
               }
               break;
            case ']':
               if(index == 0 || (index>0 && string[--index] != 2) ){
                  res = false;
               }
               break;
            default:
               break;
         }
      }
      if(index == 0 && res ){
         fprintf(stdout,"Yes\n");
      }else{
         fprintf(stdout,"No\n");
      }
   }
}
[/code]
nameofevil
New poster
 
Posts: 2
Joined: Sun Feb 25, 2007 4:01 pm

Postby asif_rahman0 » Sun Feb 25, 2007 4:57 pm

Try the following input:
Code: Select all
2
([]))

Then see, what will happen?
bye
asif_rahman0
Experienced poster
 
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

Postby nameofevil » Tue Feb 27, 2007 4:46 pm

i see, :) Thx
nameofevil
New poster
 
Posts: 2
Joined: Sun Feb 25, 2007 4:01 pm

Postby jainal cse du » Mon Mar 19, 2007 12:59 pm

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
User avatar
jainal cse du
New poster
 
Posts: 23
Joined: Thu Jul 27, 2006 2:43 pm
Location: University of Dhaka,Bangladesh

Postby shamim » Mon Mar 19, 2007 1:30 pm

You have to set top=0 before each case.
This correction will give you wrong answer.
User avatar
shamim
A great helper
 
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Dhaka

To jainal cse du ...

Postby nymo » Mon Mar 19, 2007 7:18 pm

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.
regards,
nymo
nymo
Experienced poster
 
Posts: 149
Joined: Sun Jun 01, 2003 8:58 am
Location: :)

Postby hamedv » Sat May 12, 2007 4:59 pm

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[i] != 0; i++)
{
if (s[i] == '(')
{
st++;
stack[st] = s[i];
} else
if (s[i] == '[')
{
st++;
stack[st] = s[i];
} else
if (s[i] == ')')
{
if (stack[st] != '(') b = 0;
st--;
} else
if (s[i] == ']')
{
if (stack[st] != '[') b = 0;
st--;
}
if (st < 0) b = 0;
}
if (st != 0) b = 0;
if (b)
puts("Yes");
else
puts("No");
}
}
[/code]
hamedv
Learning poster
 
Posts: 98
Joined: Mon May 07, 2007 8:30 am

PreviousNext

Return to Volume VI

Who is online

Users browsing this forum: No registered users and 0 guests