Skip to content

Commit fa4db75

Browse files
committed
add AOJ 2883 for Java
1 parent 68d8727 commit fa4db75

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

Java/AOJ/2883/Main.java

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
void run() {
5+
Scanner scan = new Scanner(System.in);
6+
while (true) {
7+
String s = scan.next();
8+
if (s.equals("."))
9+
return;
10+
String p = scan.next();
11+
int pHash = eval(s, p);
12+
int hashSum = 0;
13+
for (int i = 0; i <= 9999; i++) {
14+
int num = i;
15+
String pass = Integer.toString(num / 1000);
16+
num -= 1000 * (num / 1000);
17+
pass += Integer.toString(num / 100);
18+
num -= 100 * (num / 100);
19+
pass += Integer.toString(num / 10);
20+
num -= 10 * (num / 10);
21+
pass += Integer.toString(num);
22+
if (pHash == eval(s, pass))
23+
hashSum++;
24+
}
25+
System.out.println(pHash + " " + hashSum);
26+
}
27+
}
28+
29+
int eval(String s, String p) {
30+
// System.out.println(s);
31+
if (s.length() == 1) {
32+
switch(s) {
33+
case "a": return p.charAt(0) - '0';
34+
case "b": return p.charAt(1) - '0';
35+
case "c": return p.charAt(2) - '0';
36+
case "d": return p.charAt(3) - '0';
37+
default:
38+
throw new Error("internal error: " + s);
39+
}
40+
}
41+
int index = 1;
42+
char op = s.charAt(index++);
43+
int lhv = 0, rhv = 0;
44+
// System.out.println("left");
45+
if (s.charAt(index) != '[') {
46+
lhv = eval(s.substring(index, index + 1), p);
47+
index++;
48+
} else {
49+
int idx = index;
50+
int kakko = 1;
51+
while (kakko != 0) {
52+
idx++;
53+
if (s.charAt(idx) == '[') kakko++;
54+
else if (s.charAt(idx) == ']') kakko--;
55+
}
56+
// System.out.println("lhv: " + s + " " + index + " " + idx);
57+
// System.out.println(s.substring(index, idx + 1));
58+
lhv = eval(s.substring(index, idx + 1), p);
59+
index = idx + 1;
60+
}
61+
// System.out.println("right");
62+
if (s.charAt(index) != '[') {
63+
rhv = eval(s.substring(index, index + 1), p);
64+
} else {
65+
int idx = index;
66+
int kakko = 1;
67+
while (kakko != 0) {
68+
idx++;
69+
if (s.charAt(idx) == '[') kakko++;
70+
else if (s.charAt(idx) == ']') kakko--;
71+
}
72+
// System.out.println("rhv: " + s + " " + index + " " + idx);
73+
// System.out.println(s.substring(index, idx + 1));
74+
rhv = eval(s.substring(index, idx + 1), p);
75+
}
76+
switch(op) {
77+
case '+': return lhv | rhv;
78+
case '*': return lhv & rhv;
79+
case '^': return lhv ^ rhv;
80+
default:
81+
throw new Error("internal error");
82+
}
83+
}
84+
public static void main(String[] args) {
85+
new Main().run();
86+
}
87+
}

0 commit comments

Comments
 (0)