我已为此problem on ACM-ICPC Live archive提交了许多用Java编写的解决方案。我严格遵循编写Java解决方案的所有说明。我甚至在我的IDE上安装了JDK 6,但我总是得到Runtime error
,任何想法在这里抛出异常'因为我认为我处理了所有异常。
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);
}
}
关于我可能做错什么的任何想法?
答案 0 :(得分:0)
在process
方法中,您可以致电
D = Integer.parseInt(read.readLine().trim());
这不是最佳选择。使用扫描仪。 你的行看起来应该更像这样:
Scanner sc = new Scanner(System.in);
//...
try {
D = sc.nextInt(); // Number of test cases
N = sc.nextInt(); // Rings
U = sc.next(); // Unlocking word
}
//...
另外,请注意,可能会有多个测试用例,因此process()
或其他方法需要在for
循环内。