Java Reader class And FastReader

I was trying to do one question in java . It is a really easy question tho. I solved it using java Reader class and I am getting RE using Reader class. When I just replaced the reader class with the FastReader I got an AC. Anybody knows the reason for that?

I am just putting both the solution link and the question link here

  1. question-> Snake Procession | CodeChef
  2. Reader class Solution → Solution: 50131023 | CodeChef
  3. FastReader class Solution → Solution: 50143415 | CodeChef

In the Reader class, in the function readLine()

     public String readLine() throws IOException {
			byte[] buf = new byte[64]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1) {
				if (c == '\n') {
					if (cnt != 0) {
						break;
					} else {
						continue;
					}
				}
				buf[cnt++] = (byte)c;
			}
			return new String(buf, 0, cnt);
		}

here, in the first line it is creating byte array of 64 length so your string will be read up to only 64 characters, but in the question, it is of length 500. so, if you make slight correction and write this

public String readLine() throws IOException {
			byte[] buf = new byte[501]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1) {
				if (c == '\n') {
					if (cnt != 0) {
						break;
					} else {
						continue;
					}
				}
				buf[cnt++] = (byte)c;
			}
			return new String(buf, 0, cnt);
		}

then it will give you right answer.

2 Likes

Thanks a lot understood