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

turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

Yes.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

Could you show me with a simple example ?(concerning gets(),followed by the atoi() to obtain n...)
Thanks
To save time is to lengthen life.
turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

Ok, this line shall replace your cin >> n;

gets(chs); n = atoi(chs);

And don't forget to include stdlib.h

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

Thank you,turuthok. :D
I got AC for it now. But if it is not too bothered, would you mind explaining the reason why the online-judge System refuses to accept my code and how this change of code could be accepted again.
:(
To save time is to lengthen life.
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

you can also use

gets(a);
sscanf(a,"%d",&n);

another very easy method.
"Everything should be made simple, but not always simpler"
AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

But what is wrong with the following way to obtain an integer? Aren't they correct?
[cpp]
cin>>n;
or scanf("%d",&n);

[/cpp]
To save time is to lengthen life.
LPH
New poster
Posts: 34
Joined: Mon Nov 17, 2003 10:41 am

Re: 673Parentheses Balance

Post by LPH »

here are part of your code:
AaronWu wrote: [cpp]
cin>>n;
.
.
gets(chs);
[/cpp]
they are correct, but differ from gets(), when you use these two, they will left a '\n' in the input stream('cause they only see the number before it). after then, you use gets(), and it see this '\n' and get it back for the first string, which will be a empty string. since this problem should run when empty string entered, you get "Yes" before your first input every time you run, so it resulted to WA.

using gets() instead of these two method will read in the '\n' (though gets() didn't put it into your string).you can use atoi() or sscanf() to read the number you want from the string just read.

hope these will explain your question :)
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

Just debug with the any known debugger with watch window and you can easily find what the problem is to use scanf and gets together. There is space and endline problems with gets and scan used together.
"Everything should be made simple, but not always simpler"
AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

Thank you very much indeed.I've never noticed that before :oops:
To save time is to lengthen life.
Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

Post by Zhao Le »

I have occurred to the same question.

I can solve when using
first gets();
then atoi();

but it is C like.

I hate to both include iostream and stdio
my question is how can I turn it to C++ iostream when occurred to the multiply input.
AC makes me feels good,
But WA makes me thinks hard.
Jewel of DIU
New poster
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Location: Daffodil Univ, Bangladesh
Contact:

673: WA help plzzzz

Post by Jewel of DIU »

My prog is ok. But i have got wa plz help me.

My Input
[c]7
([])
(([()])))
([()[]()])()
)
()
)(
([)][/c]
My Output
[c]Yes
No
Yes
No
Yes
No
No[/c]

Are my output ok? :cry: :cry: :cry:
Hate WA
Visit phpBB!
the LA-Z-BOy
Learning poster
Posts: 94
Joined: Wed Jul 31, 2002 12:44 pm
Location: Dacca, Bangladesh
Contact:

Post by the LA-Z-BOy »

hi, your outputs are okay.
some tricky cases to think of:

Code: Select all

3
()()(((
<blank line>
()())))
output is:

Code: Select all

No
Yes
No
thank you.
Istiaque Ahmed [the LA-Z-BOy]
Jewel of DIU
New poster
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Location: Daffodil Univ, Bangladesh
Contact:

Thanx

Post by Jewel of DIU »

Thank u very much for ur help. My program gives Wrong output for ur 1st input. I fixed the prob and got AC.

BTW, I think ur from BUET. Do u know UTTAM? He tells me abt u.
Hate WA
Visit phpBB!
the LA-Z-BOy
Learning poster
Posts: 94
Joined: Wed Jul 31, 2002 12:44 pm
Location: Dacca, Bangladesh
Contact:

Post by the LA-Z-BOy »

yah, i'm from BUET. i know UTTAM too.
cheers
Istiaque Ahmed [the LA-Z-BOy]
20717TZ
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..

Post by 20717TZ »

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 Believe I Can - leestime.com
Post Reply

Return to “Volume 6 (600-699)”