-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCampSchedule.java
More file actions
200 lines (183 loc) · 4.5 KB
/
CampSchedule.java
File metadata and controls
200 lines (183 loc) · 4.5 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* idea
s 1110101001
-> s1, s0
t 10101
arr
1 for
-> t1, t0
10101..
|
1 0 1 0 1
x
1 1 1 0 1
x
1 1 1 0 1
c0 = 1
c1 = 1
*/
// 111011101
// => overlap: 11101 -> suffix = 1101 -> need0, need1
// t: 111011101
// t2: 1110111011101
// t3: t2 + 1101
// ti = ti-1 + 1101
//
// |T| + |T| - l => l max
// *
// 1110111011101
// y x
/*
cnt0, cnt1 of s
edge case
res = t, need0, need1
cnt0 -= cntT0, cnt1 -= cntT1
while cnt0 >= need0 and cnt1 >= need1
*/
/** #string #hash-table */
import java.util.Scanner;
class CampSchedule {
public static int base = 3;
public static long MOD = 1_000_000_007L;
public static int MAXN = 500001;
public static long[] hashArr = new long[MAXN];
// calculata hashcode of a string
public static long hashCode(String s) {
long hashValue = 0;
int sz = s.length();
int val;
for (int i = 0; i < sz; i++) {
val = s.charAt(i) - '0' + 1;
hashValue = (val + (base * hashValue) % MOD) % MOD;
hashArr[i + 1] = hashValue;
}
return hashValue;
}
public static void main(String[] args) {
// pre-calculate for calculation of hashcode
long[] mul = new long[MAXN];
mul[0] = 1;
for (int i = 1; i < MAXN; i++) {
mul[i] = (mul[i - 1] * base) % MOD;
}
// input
Scanner sc = new Scanner(System.in);
String s = sc.next();
String t = sc.next();
// count number of 0, 1
Number source = new Number(0, 0);
for (char c : s.toCharArray()) {
if (c == '0') {
source.num0 += 1;
} else {
source.num1 += 1;
}
}
// count number of 0, 1 of pattern
int sz = t.length();
int[] arr0 = new int[sz];
int[] arr1 = new int[sz];
char[] arr = t.toCharArray();
Number dest = new Number(0, 0);
for (int i = 0; i < sz; i++) {
if (arr[i] == '0') {
dest.num0 += 1;
arr0[i] = (i == 0) ? 1 : (arr0[i - 1] + 1);
arr1[i] = (i == 0) ? 0 : arr1[i - 1];
} else {
dest.num1 += 1;
arr1[i] = (i == 0) ? 1 : (arr1[i - 1] + 1);
arr0[i] = (i == 0) ? 0 : arr0[i - 1];
}
}
// edge case
if (source.num1 < dest.num1 || source.num0 < dest.num0) {
System.out.println(s);
return;
}
// find max suffix
/* res = t + suffix
int maxLength = 0;
for (int i = 1; i < t.length((); i++)) {
if (hashArr[n - i] == ((hashArr[n] - (hashArr[i] * mul[n - i]) % MOD) + MOD) % MOD) {
maxLength = n - i;
break;
}
}
Number need = new Number(0, 0);
for (int i = maxLength; i < t.length; i++) {
}
StringBuilder res = new StringBuilder(t);
t = t.substring(maxLength);
*/
long hashCode = hashCode(t);
source.num0 -= dest.num0;
source.num1 -= dest.num1;
Number need = new Number(0, 0);
String suffix = "";
for (int i = 1; i < t.length(); i++) {
suffix = maxSuffix(i, mul, arr0, arr1, t, hashCode, source, need);
if (!suffix.isEmpty()) {
break;
}
}
// calculate result
StringBuilder res = new StringBuilder(t);
if (!suffix.isEmpty()) {
while (source.num0 >= need.num0
&& source.num1 >= need.num1
&& need.num0 > 0
&& need.num1 > 0) {
res.append(suffix);
source.num0 -= need.num0;
source.num1 -= need.num1;
}
}
while (source.num0 >= dest.num0 && dest.num0 > 0 && source.num1 >= dest.num1 && dest.num1 > 0) {
res.append(t);
source.num0 -= dest.num0;
source.num1 -= dest.num1;
}
while (source.num0 > 0) {
res.append(0);
source.num0 -= 1;
}
while (source.num1 > 0) {
res.append(1);
source.num1 -= 1;
}
System.out.println(res);
}
// find max common suffix from a position of pattern and pattern
public static String maxSuffix(
int idx,
long[] mul,
int[] arr0,
int[] arr1,
String t,
long hashCode,
Number source,
Number need) {
if (t.charAt(idx) != t.charAt(0)) {
return "";
}
int n = t.length();
int len = n - idx;
int need0 = arr0[idx - 1];
int need1 = arr1[idx - 1];
if ((hashArr[len] == ((hashArr[n] - (hashArr[n - len] * mul[len]) % MOD) + MOD) % MOD)
&& source.num0 >= need0
&& source.num1 >= need1) {
need.num0 = need0;
need.num1 = need1;
return t.substring(len, n);
}
return "";
}
}
class Number {
int num0, num1;
Number(int num0, int num1) {
this.num0 = num0;
this.num1 = num1;
}
}