
673 - Parentheses Balance
Moderator: Board moderators
Re: 673 - Parentheses Balance
thanks ......... sir............ now its accepted..... 

673 WA
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.
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();
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 WA
You should print Yes or No, not yes or no.
Check input and AC output for thousands of problems on uDebug!
673(runtime)
Code: Select all
Deleted
Last edited by faraa2 on Wed Jul 10, 2013 1:22 am, edited 2 times in total.
Re: 673(runtime)
plzzzzzzzzzzzzzzzzz help me
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673(runtime)
Try input:
Code: Select all
1
))
Check input and AC output for thousands of problems on uDebug!
Re: 673(runtime)
Thank you so much....
Accepted...
Accepted...

-
- New poster
- Posts: 27
- Joined: Sat Jul 27, 2013 3:52 am
673 - Parentheses Balance
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 - Parentheses Balance
On an input like:Your code tries to read str[-1]
Code: Select all
1
)
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 27
- Joined: Sat Jul 27, 2013 3:52 am
Re: 673 - Parentheses Balance
Thanks for reply.
I had solved this......
I had solved this......

Re: 673 - Parentheses Balance
Hi Guys
I get WA for this code on parentheses Balance.
Plz Help Me!

I get WA for this code on parentheses Balance.
Plz Help Me!

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.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 - Parentheses Balance
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!
Re: 673 - Parentheses Balance
Thanks Brianfry! I Got Ac!brianfry713 wrote:You need to read line by line, there may be a blank line and reading a string will skip that.
Re: 673 Parentheses Balance - WA ?
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
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 673 Parentheses Balance - WA ?
The output should be Yes or No, not yes or no.
Check input and AC output for thousands of problems on uDebug!