673 - Parentheses Balance
Moderator: Board moderators
-
- New poster
- Posts: 33
- Joined: Tue Apr 27, 2004 7:41 pm
- Location: Santa Clara / Mountain View, CA, USA
- Contact:
673 Accepted but so long time. but I think my algorithm is..
673 Accepted but so long time. CPU=0:07.137 My God!!!!!
I think my algorithm is so simple and it should execute quickly, but I am wrong.
Why?
help me plzzzzzzz!
[cpp]
#include <stdio.h>
#include <string>
using namespace std;
int fnBalance(string s)
{
int place;
for(int i=0;s.find("[]")!=-1 || s.find("()")!=-1;i++)
{
while((place=s.find("[]"))!=-1)
s.erase(place,2);
while((place=s.find("()"))!=-1)
s.erase(place,2);
}
return s.length()==0?1:0;
}
void main()
{
FILE* fp=fopen("Input.txt","r");
int n; char c; string s;
fscanf(fp,"%d",&n);
c=fgetc(fp);//fgets(cs,MAX,fp);//skip a '\n'
for(int i=0;i<n;i++)
{
s="";//reset
while((c=fgetc(fp))!='\n' && c!=EOF)
s+=c;
printf("%s\n",fnBalance(s)?"Yes":"No");
}
}
[/cpp]
I think my algorithm is so simple and it should execute quickly, but I am wrong.
Why?
help me plzzzzzzz!
[cpp]
#include <stdio.h>
#include <string>
using namespace std;
int fnBalance(string s)
{
int place;
for(int i=0;s.find("[]")!=-1 || s.find("()")!=-1;i++)
{
while((place=s.find("[]"))!=-1)
s.erase(place,2);
while((place=s.find("()"))!=-1)
s.erase(place,2);
}
return s.length()==0?1:0;
}
void main()
{
FILE* fp=fopen("Input.txt","r");
int n; char c; string s;
fscanf(fp,"%d",&n);
c=fgetc(fp);//fgets(cs,MAX,fp);//skip a '\n'
for(int i=0;i<n;i++)
{
s="";//reset
while((c=fgetc(fp))!='\n' && c!=EOF)
s+=c;
printf("%s\n",fnBalance(s)?"Yes":"No");
}
}
[/cpp]
I Believe I Can - leestime.com
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
1. don't use STL - think how it can be done without string
2. don't post code, which is accepted - it's spoiler - anyone can submit it and get Accepted without any work - that's not fair. Please change code to description of used algorithm
Best regards
DM
2. don't post code, which is accepted - it's spoiler - anyone can submit it and get Accepted without any work - that's not fair. Please change code to description of used algorithm

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
-
- Experienced poster
- Posts: 154
- Joined: Sat Apr 17, 2004 9:34 am
- Location: EEE, BUET
Use STL sensably
Well, STL provides some important resources & good use of it should be done whenever appropriate. It eases the work (Surely you don't need to re-invent the wheel.) But again, as it is built on the object oriented structure, it's surely a bit slow. So, you really need not use STL components such as a string class which you could have easily done with some backdated though more efficient techniques such as the array based C string. In fact, you must understand which one is more important in a particular stage, efficiency or ease of coding.
You should never take more than you give in the circle of life.
673 - Accidental discovery
I recently discovered accidentally that, in every input of the judge, any string that contains the substring "[]" is correct. Am I right? I think that if this is correct the judge should provide more extra input to avoid such lucky incidents as mine.
Regards
Javi
Regards
Javi
Why WA? 673 Parentheses Balance
I got Wrong Answer too.
my program work fine too.
did you find the cause of WA?
[cpp]
#include <iostream>
#include <string>
#include <cstdio>
#include <functional>
#include <algorithm>
using namespace std;
char buff[200];
void func() {
string str;
cin.getline(buff,50);
string::size_type st;
str = buff;
int end;
do {
end = 0;
st = str.find("[]",0);
if(st==string::npos) end++;
else str.erase(str.begin()+st,str.begin()+st+2);
st = str.find("()",0);
if(st==string::npos) end++;
else str.erase(str.begin()+st,str.begin()+st+2);
} while(end < 2);
if(str.length()==0) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main() {
int n;
cin >> n;
cin.getline(buff,50);//ignore \n
while(n--) {
func();
}
}
[/cpp]
my program work fine too.
did you find the cause of WA?
[cpp]
#include <iostream>
#include <string>
#include <cstdio>
#include <functional>
#include <algorithm>
using namespace std;
char buff[200];
void func() {
string str;
cin.getline(buff,50);
string::size_type st;
str = buff;
int end;
do {
end = 0;
st = str.find("[]",0);
if(st==string::npos) end++;
else str.erase(str.begin()+st,str.begin()+st+2);
st = str.find("()",0);
if(st==string::npos) end++;
else str.erase(str.begin()+st,str.begin()+st+2);
} while(end < 2);
if(str.length()==0) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main() {
int n;
cin >> n;
cin.getline(buff,50);//ignore \n
while(n--) {
func();
}
}
[/cpp]
-
- Experienced poster
- Posts: 115
- Joined: Tue Apr 06, 2004 7:04 pm
- Location: Venezuela
Hiii htl, the bug in your code is check if the stack its empty after all the push and pops , i just add this in your code and give AC
[c]
Before :
if(error)
printf("No\n");
else
printf("Yes\n");
After :
if(error)
printf("No\n");
else
if(top == 0)
printf("Yes\n");
else
printf("No\n");[/c]
with this change you get AC
Hope it helps
Keep posting [/c]
[c]
Before :
if(error)
printf("No\n");
else
printf("Yes\n");
After :
if(error)
printf("No\n");
else
if(top == 0)
printf("Yes\n");
else
printf("No\n");[/c]
with this change you get AC
Hope it helps

Keep posting [/c]
-
- New poster
- Posts: 12
- Joined: Tue Sep 21, 2004 10:08 pm
673->parenthenses ballance WA (plz help!)
I am frustrated, I tried every possible input, but I still keep on getting WA.
Does anybody have a clue what is wrong with my code?
Is it something with the classes?
Please help!
[c]
I got AC!
[/c]
Does anybody have a clue what is wrong with my code?
Is it something with the classes?
Please help!
[c]
I got AC!
[/c]
Last edited by Ivo Sluganovic on Sat Oct 30, 2004 9:03 am, edited 1 time in total.
-
- New poster
- Posts: 12
- Joined: Tue Sep 21, 2004 10:08 pm
Try this
Input
Output
And another thing, be ware of string length. It should be at least 128
Input
Code: Select all
([)]
(
()(
[()
Code: Select all
No
No
No
No
Junayeed
-
- New poster
- Posts: 12
- Joined: Tue Sep 21, 2004 10:08 pm
673 - can't understand why WA... :(
Hi
I've seen all post in this forum and tried every input people told albout and got the rigth output, but the judge keeps saying my answer is not correct.
Can anyone help me?
here is my code:
[java]
import java.io.*;
class Main{
public static void main(String args[]){
String s;
//read the number of inputs
int n = Integer.parseInt(ReadLn(25).trim());
//for each input
for(int x=0; x<n; x++) {
//read the string
s = ReadLn(135).trim();
//if the number of elements is odd
if (s.length() % 2 != 0)
System.out.println("No"); //then it's incorrect
else {
//go through all caracters from the string
for(int i=0;i<s.length()-1;i++) {
//if the sequences () or [] exist in the string
if((s.charAt(i)== '(' && s.charAt(i+1) == ')') ||
(s.charAt(i)== '[' && s.charAt(i+1) == ']')){
//remove them
s = s.substring(0,i) + s.substring(i+2);
//and start from the beggining
i=0;
}
}
if(s.equals("") || s.equals("()") || s.equals("[]"))
System.out.println("Yes");
else
System.out.println("No");
}
}
}
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
}[/java]
I've seen all post in this forum and tried every input people told albout and got the rigth output, but the judge keeps saying my answer is not correct.
Can anyone help me?
here is my code:
[java]
import java.io.*;
class Main{
public static void main(String args[]){
String s;
//read the number of inputs
int n = Integer.parseInt(ReadLn(25).trim());
//for each input
for(int x=0; x<n; x++) {
//read the string
s = ReadLn(135).trim();
//if the number of elements is odd
if (s.length() % 2 != 0)
System.out.println("No"); //then it's incorrect
else {
//go through all caracters from the string
for(int i=0;i<s.length()-1;i++) {
//if the sequences () or [] exist in the string
if((s.charAt(i)== '(' && s.charAt(i+1) == ')') ||
(s.charAt(i)== '[' && s.charAt(i+1) == ']')){
//remove them
s = s.substring(0,i) + s.substring(i+2);
//and start from the beggining
i=0;
}
}
if(s.equals("") || s.equals("()") || s.equals("[]"))
System.out.println("Yes");
else
System.out.println("No");
}
}
}
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
}[/java]