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

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: 673 - Parentheses Balance

Post by raj » Fri Feb 15, 2013 6:37 pm

thanks ......... sir............ now its accepted..... :)

dallen
New poster
Posts: 7
Joined: Mon Apr 22, 2013 1:06 am
Location: Asunción, Paraguay

673 WA

Post by dallen » Fri Apr 26, 2013 10:08 pm

I'm passing most test cases I found in the forums and the test cases in the problem. Can anyone give a hand debugging or with tricky test cases to try.. Thanks in advance.

Here's my code.

Code: Select all

//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;

public class Main {

	static final Integer PAREN = new Integer(0);
	static final Integer BRACKET = new Integer(1);

	public static void main(String[] args) /*throws FileNotFoundException*/ {
//		final long startTime = System.currentTimeMillis();
//		System.setIn(new FileInputStream(new File("test.txt")));

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine().trim());

		StringBuilder out = new StringBuilder(n);

		while (sc.hasNextLine()) {
			String line = sc.nextLine();

			if (balance(line)) {
				out.append("yes\n");
			} else {
				out.append("no\n");
			}
		}

		// delete the last '\n'
		out.deleteCharAt(out.length() - 1);
		System.out.println(out.toString());
		//System.out.println(System.currentTimeMillis() - startTime);
	}

	private static boolean balance(String line) {
		char[] chars = line.toCharArray();
		Stack<Integer> stack = new Stack<Integer>();

		for (char c : chars) {
			if (c == '(') {
				stack.push(PAREN);
			} else if (c == '[') {
				stack.push(BRACKET);
			} else if (c == ')') {
				if (stack.isEmpty())
					return false;
				if (stack.pop() != PAREN)
					return false;
			} else if (c == ']') {
				if (stack.isEmpty())
					return false;
				if (stack.pop() != BRACKET)
					return false;
			}
		}

		return stack.isEmpty();
	}
}


brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 673 WA

Post by brianfry713 » Fri May 17, 2013 3:53 am

You should print Yes or No, not yes or no.
Check input and AC output for thousands of problems on uDebug!

faraa2
New poster
Posts: 3
Joined: Sat Jul 06, 2013 11:12 am

673(runtime)

Post by faraa2 » Sat Jul 06, 2013 11:17 am

Code: Select all

Deleted
Last edited by faraa2 on Wed Jul 10, 2013 1:22 am, edited 2 times in total.

faraa2
New poster
Posts: 3
Joined: Sat Jul 06, 2013 11:12 am

Re: 673(runtime)

Post by faraa2 » Sat Jul 06, 2013 7:51 pm

plzzzzzzzzzzzzzzzzz help me

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 673(runtime)

Post by brianfry713 » Mon Jul 08, 2013 11:11 pm

Try input:

Code: Select all

1
))
Check input and AC output for thousands of problems on uDebug!

faraa2
New poster
Posts: 3
Joined: Sat Jul 06, 2013 11:12 am

Re: 673(runtime)

Post by faraa2 » Mon Jul 08, 2013 11:44 pm

Thank you so much....
Accepted... :D

shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

673 - Parentheses Balance

Post by shikhorroy » Sat Jul 27, 2013 4:02 am

Where is the problem??? Please help me........

Code: Select all

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int T;
    string str;
    scanf("%d%*c",&T);
    while(T--)
    {
        getline(cin, str);

        int i = 0;
        while(!str.empty())
        {
            if(str[i] == ')')
            {
                i--;
                if(str[i] == '(')
                {
                    str.erase(i,2);
                    continue;
                }
                else
                {
                    printf("No\n");
                    break;
                }
            }
            else if(str[i] == ']')
            {
                i--;
                if(str[i] == '[')
                {
                    str.erase(i,2);
                    continue;
                }
                else
                {
                    printf("No\n");
                    break;
                }
            }
            else   i++;
        }
        if(str.empty())    printf("Yes\n");
        str.clear();
    }
    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 673 - Parentheses Balance

Post by brianfry713 » Mon Jul 29, 2013 4:58 am

On an input like:

Code: Select all

1
)
Your code tries to read str[-1]
Check input and AC output for thousands of problems on uDebug!

shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

Re: 673 - Parentheses Balance

Post by shikhorroy » Fri Aug 02, 2013 5:27 pm

Thanks for reply.
I had solved this...... :)

Salam!
New poster
Posts: 7
Joined: Tue Sep 10, 2013 7:14 pm

Re: 673 - Parentheses Balance

Post by Salam! » Wed Sep 11, 2013 12:06 am

Hi Guys :D
I get WA for this code on parentheses Balance.
Plz Help Me! :wink:

Code: Select all

Tnx... I Got Ac  :lol:  :lol: 
}
Last edited by Salam! on Wed Sep 11, 2013 11:53 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 673 - Parentheses Balance

Post by brianfry713 » Wed Sep 11, 2013 10:02 pm

You need to read line by line, there may be a blank line and reading a string will skip that.
Check input and AC output for thousands of problems on uDebug!

Salam!
New poster
Posts: 7
Joined: Tue Sep 10, 2013 7:14 pm

Re: 673 - Parentheses Balance

Post by Salam! » Wed Sep 11, 2013 11:54 pm

brianfry713 wrote:You need to read line by line, there may be a blank line and reading a string will skip that.
Thanks Brianfry! I Got Ac!

alamcse09
New poster
Posts: 1
Joined: Sun Oct 13, 2013 9:44 am

Re: 673 Parentheses Balance - WA ?

Post by alamcse09 » Sun Oct 13, 2013 5:34 pm

Code: Select all

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

bool check(char *in)
{
	if(strcmp(in,"")==0)
		return true;
	if(in[0]=='\0')
		return true;
	else
	{
		char temp[169];
		int i,start,end,j;
		start=end=0;
		int count;
		char ch;
		bool f=true;
		for(i=0;i<strlen(in);i++)
		{
			ch=in[i];
			start=i;
			if(ch==')' || ch==']')
				return false;
			if(ch=='(')
			{
				count=1;
				for(j=i+1; ;j++)
				{
					if(j>=strlen(in))
					{
						return false;
					}
					if(in[j]==')')
					{
						count--;
					}
					else if(in[j]=='(')
					{
						count++;
					}
					if(count==0)
					{
						end=j;
						i=j;
						break;
					}
				}
			}
			else if(ch=='[')
			{
				count=1;
				for(j=i+1; ;j++)
				{
					if(j>=strlen(in))
					{
						return false;
					}
					if(in[j]==']')
					{
						count--;
					}
					else if(in[j]=='[')
					{
						count++;
					}
					if(count==0)
					{
						end=j;
						i=j;
						break;
					}
				}
			}
			int k=0;
			temp[k]='\0';
			int l=0;
			for(k=start+1,l;k<end;k++,l++)
			{
				temp[l]=in[k];
			}
			temp[l]='\0';
			f=f&&check(temp);
		}
		return f;
	}
}


int main()
{
	int test;
	char input[169];
	cin>>test;
	while(test--)
	{
		scanf("%s",&input);
		bool k=check(input);
		if(k)
			cout<<"yes\n";
		else
			cout<<"no\n";
	}
	return 0;
}

GETTING WA DON'T KNOW WHY,IT PASSED MANY TEST CASES

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 673 Parentheses Balance - WA ?

Post by brianfry713 » Mon Oct 14, 2013 8:31 pm

The output should be Yes or No, not yes or no.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 6 (600-699)”