673 - Parentheses Balance

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

Moderator: Board moderators

Abid_CU
New poster
Posts: 1
Joined: Tue Nov 10, 2015 8:42 pm

Re: 673 - Parentheses Balance, please see whats the problem here?

Post by Abid_CU »

Code: Select all

#include<stdio.h>
#include<string.h>
int main()
{
    int t,i,j,len,flag;
    char s[5000], tm[5000];
    scanf("%d", &t);
    getchar();
    while(t--)
    {
        gets(s);
        len= strlen(s);
        flag=0;
        j=0;
        for(i=0; i<len; i++)
        {
            if(s[i]=='('||s[i]=='[') tm[j++]= s[i];
            else
            {
                if(s[i]==')')
                {
                    if(tm[j-1]=='(')
                    {
                        j--;
                    }
                    else
                    {
                        printf("No\n");
                        flag=1;
                        break;
                    }
                }
                else if(s[i]==']')
                {
                    if(tm[j-1]=='[')
                    {
                        j--;
                    }
                    else
                    {
                        printf("No\n");
                        flag=1;
                        break;
                    }
                }
            }
        }
        if(j==0 && flag==0) printf("Yes\n");
        else if(j!=0) printf("No\n");
    }
    return 0;
}
nasif2587
New poster
Posts: 2
Joined: Thu Jan 07, 2016 12:48 pm

Re: 673 - Parentheses Balance

Post by nasif2587 »

removed after ac
Ishraq_Nibir
New poster
Posts: 2
Joined: Fri Mar 10, 2017 10:53 pm

Re: 673 - Parentheses Balance

Post by Ishraq_Nibir »

What is the problem in my code..getting wrong answer
#include <stdio.h>
#include <string.h>
int main()
{
int n,i,j,k,l,count,mount,len,first,third;
char str[130];
scanf("%d",&n);
getchar();
for(i=0;i<n;i++){
gets(str);
len=strlen(str);
count=0;mount=0;first=0;third=0;
for(j=0;j<len;j++){
if(str[j]=='('){
first=1;
break;
}
if(str[j]==')'){
first=3;
break;
}
}
for(j=0;j<len;j++){
if(str[j]=='['){
third=1;
break;
}
if(str[j]==']'){
third=3;
break;
}
}
if(first!=3&&third!=3){
for(j=0;j<len;j++){
if(str[j]=='(')
count++;
if(str[j]==')')
count--;
if(str[j]=='[')
mount++;
if(str[j]==']')
mount--;
}
if(count==0&&mount==0)
printf("Yes\n");
else
printf("No\n");
}
else
printf("No\n");
}
return 0;
}
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 673 - Parentheses Balance

Post by lighted »

Check your code here. https://www.udebug.com/UVa/673
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Post Reply

Return to “Volume 6 (600-699)”