-
Notifications
You must be signed in to change notification settings - Fork 0
/
SenseOfBeauty.java
107 lines (83 loc) · 2.43 KB
/
SenseOfBeauty.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class SenseOfBeauty
{
public static void main(String[] argv) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(getLine(br)) + 1;
String A = getLine(br);
String B = getLine(br);
Set<String> cache = new HashSet<>();
Stack<Integer> solution = new Stack<>();
int rc = solutionRecursive(A,B,0,0,0,0,solution,cache);
if (rc == -1) {
System.out.println("Impossible");
}
else {
for (int k : solution) {
System.out.print(k);
}
System.out.println("");
}
}
static int solutionRecursive(String A, String B, int posA, int posB, int r1, int r2, Stack<Integer> result, Set<String> cache ) {
if (cache.contains(posA+" "+posB)) {
return -1;
}
cache.add(posA+" "+posB);
if (Math.abs(r1-r2) > 1) {
return -1;
}
else if (posA == A.length() && posB == B.length()) {
return 0;
}
if (posA < A.length()) {
int dr1 = 0;
int dr2 = 0;
if ( A.charAt(posA) == '0' ) {
dr1 = 1;
}
else {
dr2 = 1;
}
result.push(1);
int rc = solutionRecursive(A, B, posA + 1, posB, r1 + dr1, r2 + dr2, result, cache);
if (rc == -1) {
result.pop();
}
else {
return rc;
}
}
if (posB < B.length()) {
int dr1 = 0;
int dr2 = 0;
if ( B.charAt(posB) == '0' ) {
dr1 = 1;
}
else {
dr2 = 1;
}
result.push(2);
int rc = solutionRecursive(A, B, posA, posB + 1, r1 + dr1, r2 + dr2, result, cache);
if (rc == -1) {
result.pop();
}
else {
return rc;
}
}
return -1;
}
public static String getLine(BufferedReader br) {
String s = null;
try {
s = br.readLine();
}
catch (Exception e) {
}
return s;
}
}