Runtime error on ACM-ICPC Live Archive 3242 - Da Vinci's Cryptex

391 views Asked by At

I've submitted many solutions written in Java for this problem on ACM-ICPC Live archive. I followed, strictly all the instructions of writing Java solutions. I even installed JDK 6 on my IDE but I always get Runtime error, any idea what is throwing exception here 'cos I think I handled all exceptions.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

class Main {

    BufferedReader read;
    BufferedWriter write;
    Integer D, N;
    String U, S;
    ArrayList<String> cryptex;

    public static void main(String[] args) {

        new Main().solve();

    }

    private void solve() {

        read = new BufferedReader(new InputStreamReader(System.in));
        write = new BufferedWriter(new OutputStreamWriter(System.out));

        process();

        try {
            read.close();
            write.flush();
            write.close();
        } catch (IOException ex) {
            return;
        }
    }

    private void process() {

        try {
            D = Integer.parseInt(read.readLine().trim());
        } catch (IOException ex) {
            return;
        }

        for (int i = 0; i < D; i++) {
            try {
                String[] params = read.readLine().trim().split("\\s+");
                if (params.length != 3) {
                    return;
                }

                N = Integer.parseInt(params[0]);
                U = params[1];
                S = params[2];

                cryptex = new ArrayList<String>(N);

                for (int j = 0; j < N; j++) {
                    cryptex.add(read.readLine().trim());
                }

                try {
                    write.write(U + " " + solveCase());
                } catch (Exception ex) {
                    return;
                }

                write.newLine();
                read.readLine();
            } catch (IOException ex) {
                return;
            }

        }
    }

    private String solveCase() throws Exception {
        Integer n = null, f = null, add = null;
        for (int i = 0; i < N; i++) {
            if (S.charAt(i) != '_') {
                n = cryptex.get(i).indexOf(S.charAt(i));
                f = cryptex.get(i).indexOf(U.charAt(i));
                add = n - f;
                break;
            }
        }
        if (n == null || f == null || add == null) {
            throw new Exception("Incorrect test case exception.");
        }
        char[] ret = S.toCharArray();
        for (int i = 0; i < N; i++) {
            f = cryptex.get(i).indexOf(U.charAt(i));
            n = (add + f + 26) % 26;
            ret[i] = cryptex.get(i).charAt(n);
        }

        return new String(ret);
    }

}

Any idea on what I might be doing wrong?

1

There are 1 answers

0
mbomb007 On

In your process method, you call

D = Integer.parseInt(read.readLine().trim());

This is not optimal. Use a scanner. Your line should look more like this:

Scanner sc = new Scanner(System.in);
//...
try {
    D = sc.nextInt(); // Number of test cases
    N = sc.nextInt(); // Rings
    U = sc.next();    // Unlocking word
}
//...

Also, note that there will likely be more than one test case, so process() or some other method will need to be inside a for loop.