673 - can't understand why WA... :(

Write here if you have problems with your Java source code

Moderator: Board moderators

673 - can't understand why WA... :(

Postby .pandre. » Wed Nov 10, 2004 11:50 am

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]
.pandre.
New poster
 
Posts: 9
Joined: Mon Nov 08, 2004 10:58 pm
Location: Coimbra, Portugal

Postby Maniac » Wed Nov 10, 2004 4:58 pm

hmmm, everything looks correct to me. Just some comments:
Have you tried the special case of empty lines as input? Maybe you should also exclude the '\r' character in the readline function? And doesn't s.substring(i+2) give an exception when applied at the end of the string s?
Maniac
Experienced poster
 
Posts: 105
Joined: Tue Oct 14, 2003 3:24 pm
Location: Utrecht, Holland

Postby .pandre. » Wed Nov 10, 2004 10:36 pm

[/quote]
Have you tried the special case of empty lines as input?
[/quote]

yes I have tried the empty string case and had the correct answer ("Yes")

And doesn't s.substring(i+2) give an exception when applied at the end of the string s?


no, no problem there :(


but I finnally got AC, i think the problem was that the number that I was passing to ReadLn() was too small, (in the accepted version, I used 50 for the number n (wow, they really use BIG numbers):
int n = Integer.parseInt(ReadLn(50).trim())

really stupid thing, don't you think?

anyway, thanks a lot for your time, i really apreciated your help
.pandre.
New poster
 
Posts: 9
Joined: Mon Nov 08, 2004 10:58 pm
Location: Coimbra, Portugal

Postby Maniac » Thu Nov 11, 2004 1:32 am

I don't think the judge uses numbers of more than 25 digits for the number of test cases.. are you sure that this is the only change you made before getting AC? Well, congrats anyway and no problem, keep up the good work :)
Maniac
Experienced poster
 
Posts: 105
Joined: Tue Oct 14, 2003 3:24 pm
Location: Utrecht, Holland

Postby .pandre. » Thu Nov 11, 2004 2:50 am

Maniac wrote:I don't think the judge uses numbers of more than 25 digits for the number of test cases.. are you sure that this is the only change you made before getting AC? Well, congrats anyway and no problem, keep up the good work :)


yes, it was the only change ! quite weird, ins't it? well but that's an AC and that's what matters when we're so worried
once again thanks
.pandre.
New poster
 
Posts: 9
Joined: Mon Nov 08, 2004 10:58 pm
Location: Coimbra, Portugal

Postby Maniac » Thu Nov 11, 2004 1:39 pm

for your convenience and to avoid these problems in the future, there is also a readline function witch doesn't take a length-parameter but just reads a line of arbitrary length. See the forum-thread 'Java useful methods to save time'.
Maniac
Experienced poster
 
Posts: 105
Joined: Tue Oct 14, 2003 3:24 pm
Location: Utrecht, Holland


Return to Java

Who is online

Users browsing this forum: No registered users and 1 guest

cron