673 - Parentheses Balance
Moderator: Board moderators
Re: 673Parentheses Balance
here are part of your code:
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![:)](./images/smilies/icon_smile.gif)
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.AaronWu wrote: [cpp]
cin>>n;
.
.
gets(chs);
[/cpp]
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
![:)](./images/smilies/icon_smile.gif)
-
- New poster
- Posts: 32
- Joined: Thu Jul 31, 2003 6:21 am
- Location: Daffodil Univ, Bangladesh
- Contact:
673: WA help plzzzz
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:](./images/smilies/icon_cry.gif)
My Input
[c]7
([])
(([()])))
([()[]()])()
)
()
)(
([)][/c]
My Output
[c]Yes
No
Yes
No
Yes
No
No[/c]
Are my output ok?
![:cry:](./images/smilies/icon_cry.gif)
![:cry:](./images/smilies/icon_cry.gif)
![:cry:](./images/smilies/icon_cry.gif)
Hate WA
Visit phpBB!
Visit phpBB!
-
- Learning poster
- Posts: 94
- Joined: Wed Jul 31, 2002 12:44 pm
- Location: Dacca, Bangladesh
- Contact:
hi, your outputs are okay.
some tricky cases to think of:
output is:
thank you.
some tricky cases to think of:
Code: Select all
3
()()(((
<blank line>
()())))
Code: Select all
No
Yes
No
Istiaque Ahmed [the LA-Z-BOy]
-
- New poster
- Posts: 32
- Joined: Thu Jul 31, 2003 6:21 am
- Location: Daffodil Univ, Bangladesh
- Contact:
Thanx
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.
BTW, I think ur from BUET. Do u know UTTAM? He tells me abt u.
Hate WA
Visit phpBB!
Visit phpBB!
-
- Learning poster
- Posts: 94
- Joined: Wed Jul 31, 2002 12:44 pm
- Location: Dacca, Bangladesh
- Contact:
-
- 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