-
Notifications
You must be signed in to change notification settings - Fork 0
/
TernaryXOR.java
67 lines (65 loc) · 1.64 KB
/
TernaryXOR.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
/** https://codeforces.com/contest/1328/problem/C #implementation */
import java.util.Scanner;
public class TernaryXOR {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
for (int t = 0; t < testcases; t++) {
int n = sc.nextInt();
char[] s = sc.next().toCharArray();
char[] a = new char[n];
char[] b = new char[n];
a[0] = b[0] = '1';
// provided that A <= B.
boolean aLessThanB = false;
for (int i = 1; i < n; i++) {
if (s[i] == '2') {
if (aLessThanB) {
a[i] = '2';
b[i] = '0';
} else {
a[i] = '1';
b[i] = '1';
}
} else if (s[i] == '1') {
if (!aLessThanB) {
aLessThanB = true;
a[i] = '0';
b[i] = '1';
} else {
a[i] = '1';
b[i] = '0';
}
} else {
a[i] = b[i] = '0';
}
}
/* better implementation
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
// a Less Than b
a[i] = '0';
b[i] = '1';
for (int j = i + 1; j < n; j++)
a[j] = s[j];
b[j] = '0';
}
break;
}
else {
a[i] = b[i] = '0' + (s[i] - '0') / 2;
}
}
*/
// result
for (int i = 0; i < n; i++) {
System.out.print(a[i]);
}
System.out.println();
for (int i = 0; i < n; i++) {
System.out.print(b[i]);
}
System.out.println();
}
}
}