Page 14 of 17

Re: 673 - Parentheses Balance

Posted: Fri Feb 15, 2013 6:37 pm
by raj
thanks ......... sir............ now its accepted..... :)

673 WA

Posted: Fri Apr 26, 2013 10:08 pm
by dallen
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();
	}
}


Re: 673 WA

Posted: Fri May 17, 2013 3:53 am
by brianfry713
You should print Yes or No, not yes or no.

673(runtime)

Posted: Sat Jul 06, 2013 11:17 am
by faraa2

Code: Select all

Deleted

Re: 673(runtime)

Posted: Sat Jul 06, 2013 7:51 pm
by faraa2
plzzzzzzzzzzzzzzzzz help me

Re: 673(runtime)

Posted: Mon Jul 08, 2013 11:11 pm
by brianfry713
Try input:

Code: Select all

1
))

Re: 673(runtime)

Posted: Mon Jul 08, 2013 11:44 pm
by faraa2
Thank you so much....
Accepted... :D

673 - Parentheses Balance

Posted: Sat Jul 27, 2013 4:02 am
by shikhorroy
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;
}

Re: 673 - Parentheses Balance

Posted: Mon Jul 29, 2013 4:58 am
by brianfry713
On an input like:

Code: Select all

1
)
Your code tries to read str[-1]

Re: 673 - Parentheses Balance

Posted: Fri Aug 02, 2013 5:27 pm
by shikhorroy
Thanks for reply.
I had solved this...... :)

Re: 673 - Parentheses Balance

Posted: Wed Sep 11, 2013 12:06 am
by Salam!
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: 
}

Re: 673 - Parentheses Balance

Posted: Wed Sep 11, 2013 10:02 pm
by brianfry713
You need to read line by line, there may be a blank line and reading a string will skip that.

Re: 673 - Parentheses Balance

Posted: Wed Sep 11, 2013 11:54 pm
by Salam!
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!

Re: 673 Parentheses Balance - WA ?

Posted: Sun Oct 13, 2013 5:34 pm
by alamcse09

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

Re: 673 Parentheses Balance - WA ?

Posted: Mon Oct 14, 2013 8:31 pm
by brianfry713
The output should be Yes or No, not yes or no.