-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1007. Code Words.java
More file actions
139 lines (108 loc) · 2.92 KB
/
1007. Code Words.java
File metadata and controls
139 lines (108 loc) · 2.92 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.Scanner;
public class CodeWords {
/* '2' skip j. */
public static void printWord(byte[] chars, int len, int replaceId, char replaceChar) {
for (int i = 0; i < len; i++) {
if (i == replaceId && replaceChar == '2')
continue;
if (i == replaceId)
System.out.append(replaceChar);
System.out.append((char)chars[i]);
}
if (replaceId == len)
System.out.append(replaceChar);
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
int fullSum = 0;
/* rightCount[i] = number of 1's to the right of i, i included */
int rightCount[] = new int[N + 1];
String word = null;
byte[] chars = new byte[2001];
int len = 0;
while (true) {
try {
word = sc.nextLine();
} catch (Exception e) {
System.exit(0);
}
len = 0;
if (word.isEmpty())
continue;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == '0')
chars[len++] = '0';
else if (word.charAt(i) == '1')
chars[len++] = '1';
}
rightCount[len - 1] = chars[len - 1] - '0';
fullSum = len * rightCount[len - 1];
for (int j = len - 2; j >= 0; j--) {
rightCount[j] = rightCount[j + 1] + (chars[j] - '0');
fullSum += (j + 1)*(chars[j] - '0');
}
/* A char is changed */
if (len == N) {
/* if it's already a multiple of (N + 1) or 0,
* nothing to do
*/
if ((fullSum == 0) || (fullSum % (N + 1) == 0)) {
printWord(chars, len, -1, '0');
} /* find to replace an 1 to 0 */
else {
for (int j = 0; j < len; j++) {
if ((fullSum - (j + 1) * (chars[j] - '0')) % (N + 1) == 0) {
chars[j] = '0';
printWord(chars, len, -1, '0');
break;
}
}
}
}
/* A char is removed */
else if (len == N - 1) {
if ((fullSum == 0) || (fullSum % (N + 1) == 0)) {
printWord(chars, len, len, '0');
}
else {
/* Find where to put an 0 or one so it will work */
for (int j = 0; j <= len; j++) {
/* Try to append it at the end */
if (j == len) {
if ((fullSum + (j + 1)) % (N + 1) == 0)
printWord(chars, len, len, '1');
}
/* Try 0 */
else if ((fullSum + rightCount[j]) % (N + 1) == 0) {
printWord(chars, len, j, '0');
break;
}
/* Try 1 */
else if ((fullSum + rightCount[j] + (j + 1)) % (N + 1) == 0) {
printWord(chars, len, j, '1');
break;
}
}
}
}
/* char is added */
else {
/* find a char to delete */
for (int j = 0; j < len; j++) {
int temp;
if (j == len - 1)
temp = 0;
else
temp = rightCount[j + 1];
if ((fullSum - temp - (j + 1) * (chars[j] - '0')) % (N + 1) == 0) {
printWord(chars, len, j, '2');
break;
}
}
}
}
}
}