673 - Parentheses Balance
Moderator: Board moderators
-
- New poster
- Posts: 4
- Joined: Mon Jun 16, 2008 7:21 am
- Location: Kolkata, India
- Contact:
Re: 673 - Parentheses Balance
Onek Dhonnobad, Jan. Ekhon solution Accepted hoyeche. Tobe ektu bistarito bhabe karon ta bojhate parbey ? After all, maximum string length to 128 bola chilo, tai na?
Jodi Tor Dak Shune Keu Naa Ashey....
Tobey Ekla Cholo Re....
Tobey Ekla Cholo Re....
-
- Experienced poster
- Posts: 103
- Joined: Tue Mar 25, 2008 11:00 pm
- Location: IUT-OIC, DHAKA, BANGLADESH
- Contact:
Re: 673 - Parentheses Balance
Samudra Banerjee wrote
pardon me....its a global forum.U should think about that. your language should be English.Onek Dhonnobad, Jan. Ekhon solution Accepted hoyeche. Tobe ektu bistarito bhabe karon ta bojhate parbey ? After all, maximum string length to 128 bola chilo, tai na?
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................
It is more tough to become a good person.
I am trying both...............................
-
- New poster
- Posts: 4
- Joined: Mon Jun 16, 2008 7:21 am
- Location: Kolkata, India
- Contact:
Re: 673 - Parentheses Balance
Ya..I am really sorry....
What I wanted to say was that the max string length was given to be 128. So what was the need to set the array size to 130 ? 129 should have been enough, isn't it ?
What I wanted to say was that the max string length was given to be 128. So what was the need to set the array size to 130 ? 129 should have been enough, isn't it ?
Jodi Tor Dak Shune Keu Naa Ashey....
Tobey Ekla Cholo Re....
Tobey Ekla Cholo Re....
-
- New poster
- Posts: 20
- Joined: Thu Jan 17, 2008 10:47 pm
- Location: India
pls help WA :(
Code: Select all
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
int n;
stack<char> s;
string str;
cin>>n;
for(int i=0;i<n;i++)
{
getline(cin,str);
if(str.length()==0)
goto end;
s.push(str[0]);
for(int j=1;j<str.length();j++)
{
if(s.empty())
s.push(str[j]);
else
{
if((s.top()=='(' && str[j]==')') || (s.top()=='[' && str[j]==']'))
s.pop();
else
s.push(str[j]);
}
}
end:
if(s.empty())
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
while(!s.empty())
s.pop();
// cout<<s.empty()<<endl;
str.clear();
}
// system("pause");
}
"if u r goin thru hell, keep goin"
-
- New poster
- Posts: 9
- Joined: Thu Aug 21, 2008 3:08 am
- Location: IUT
Parentheses Balance WA
why WA
please help
here is my code
please help
here is my code
Code: Select all
#include<stdio.h>
int main()
{
int a,i,j,flag,t;
char arr[150];
char ch;
freopen("in.txt","r",stdin);
while(scanf("%d%*c",&a)==1)
{
for(i=0;i<a;i++)
{
t=0;
j=0;
flag=0;
while(1)
{
scanf("%c",&ch);
if(ch=='\n')
{
break;
}
else if(ch==' ')
continue;
else if(ch==')')
{
if(!j || arr[j-1]!='(')
{
flag=1;
}
j--;
}
else if(ch==']')
{
if(!j || arr[j-1]!='[')
{
flag=1;
}
j--;
}
else
{
arr[j]=ch;
j++;
}
t++;
}
if(flag || j ||t==0)
printf("No\n");
else
printf("Yes\n");
}
}
return 0;
}
wanna be notorious....
Re: 673 - Parentheses Balance
Hai guys. I dont reply to any post but do keep in mind that empty string has to give out "Yes" . I did this mistake and was looking into it for 3 days
-
- New poster
- Posts: 6
- Joined: Mon Dec 15, 2008 3:34 pm
- Location: Surabaya, Indonesia
Re: 673 - WA Please Help
Help me, i don't know why i got WA, i already test my code with many input...
Code: Select all
got AC .. code removed
Last edited by anton_indrawan on Tue Dec 16, 2008 5:21 pm, edited 1 time in total.
Re: 673 - Parentheses Balance
Here are a couple of examples where your program goes wrong:
Your algorithm is too complex for such a simple problem. You only need a simple stack to solve it.
Code: Select all
2
[]
(([())])
-
- New poster
- Posts: 6
- Joined: Mon Dec 15, 2008 3:34 pm
- Location: Surabaya, Indonesia
Re: 673 - Parentheses Balance
Hai mf, thank about your information..
I got AC with 0.050 seconds using the stack (include<stack>)
Is it necessary to create own class stack ?
Which one is faster, own class stack or using the lib one ?
Thx Before.. Sorry for my language
I got AC with 0.050 seconds using the stack (include<stack>)
Is it necessary to create own class stack ?
Which one is faster, own class stack or using the lib one ?
Thx Before.. Sorry for my language
Re: 673 - Parentheses Balance
No. And why do you even ask? If you feel like writing your own stack, just do it, it's very simple:Is it necessary to create own class stack ?
Code: Select all
int stk[1000], top = 0; // the stack
push an element x: stk[top++] = x;
popping an element: stk[--top]
Re: 673 - Parentheses Balance
Could anyone help me, i don't know why i got WA. Here is my code in java.
import java.io.*;
import java.util.*;
public class Main
{
public static boolean oklepaji(String niz)
{
Stack<Character> s = new Stack<Character>();
for (int i = 0; i < niz.length(); i++)
{
char znak = niz.charAt(i);
if (znak == '(' || znak == '[') s.push(znak);
else if (znak == ')' && !s.empty() && s.peek() == '(') s.pop();
else if (znak == ']' && !s.empty() && s.peek() == '[') s.pop();
else return false;
}
if (!s.empty()) return false;
return true;
}
public static void main(String[] args) throws IOException
{
BufferedReader vhod = new BufferedReader(new
InputStreamReader(System.in));
int n = Integer.parseInt(vhod.readLine());
String[] tab = new String[n];
for (int i = 0; i < n; i++)
{
tab = vhod.readLine();
}
System.out.println();
for (int i = 0; i < n; i++)
{
if (oklepaji(tab)) System.out.println("Yes");
else System.out.println("No");
}
}
}
import java.io.*;
import java.util.*;
public class Main
{
public static boolean oklepaji(String niz)
{
Stack<Character> s = new Stack<Character>();
for (int i = 0; i < niz.length(); i++)
{
char znak = niz.charAt(i);
if (znak == '(' || znak == '[') s.push(znak);
else if (znak == ')' && !s.empty() && s.peek() == '(') s.pop();
else if (znak == ']' && !s.empty() && s.peek() == '[') s.pop();
else return false;
}
if (!s.empty()) return false;
return true;
}
public static void main(String[] args) throws IOException
{
BufferedReader vhod = new BufferedReader(new
InputStreamReader(System.in));
int n = Integer.parseInt(vhod.readLine());
String[] tab = new String[n];
for (int i = 0; i < n; i++)
{
tab = vhod.readLine();
}
System.out.println();
for (int i = 0; i < n; i++)
{
if (oklepaji(tab)) System.out.println("Yes");
else System.out.println("No");
}
}
}
Re: 673 - Parentheses Balance
Probably because you have an unnecessary System.out.println();
-
- New poster
- Posts: 22
- Joined: Mon Sep 19, 2005 4:58 am
- Contact:
Re: 673 (dont open a new thread.......just search the prob. num.
I went through problem description looking for sort and ordered in addition to balanced output. I found none, hence "([)]" should be Yes. Meaning the input can be jumbled as long as it balances in the end.
kbr_iut wrote:dont open a new thread as there is already many threads...think about that.
however ur program gives wrong output for this set of input.
input:ur output is:Code: Select all
7 ([]) (([()]))) ([()[]()])() ([)] ( ()( [()
My AC program gives:Code: Select all
Yes No Yes Yes No No No
Hope it will help.Code: Select all
Yes No Yes No No No No
-
- New poster
- Posts: 6
- Joined: Mon Dec 15, 2008 3:34 pm
- Location: Surabaya, Indonesia
Re: 673 - Parentheses Balance
Hay mf
thx for your information, you are very experienced ... ^__^
thx for your information, you are very experienced ... ^__^