-
Notifications
You must be signed in to change notification settings - Fork 1
/
Circular Palindromes.kt
244 lines (195 loc) · 7.82 KB
/
Circular Palindromes.kt
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import java.io.*
import java.math.*
import java.text.*
import java.util.*
import java.util.regex.*
import kotlin.math.min
/**
* for each index, try expanding and find the palindrome centered at that (Manachers)
*
* let c be the center, R the right edge of the palindrome found
*
* for the next index within the right edge, find the mirror left of center
*
* p[i] is Min of P[mirror], distance to right edge
* only need to expand from current index beyond p[i]
*
* Returns the max size palindromes of each rotated strings
*
* (modifieid manachers) -- first compress the input
* rotate using the compressed input
*/
fun getPalindromSizes(input: String): Array<Int> {
val inputLength = input.length
val rotatedSizes = Array(input.length) { 0 }
//compress the repeated chars
val compressedSizes = ArrayList<Int>()
val compressedInput = ArrayList<Char>()
val paddedString = StringBuilder()
var first = 0
compressedSizes.add(0)
paddedString.append('#')
while (first < input.length) {
var second = first + 1
while (second < input.length && input[first] == input[second]) {
second++
}
compressedInput.add(input[first])
paddedString.append(input[first])
compressedSizes.add(second - first)
paddedString.append('#')
compressedSizes.add(0)
first = second
}
val fold = input.length > 1000 && compressedInput.size > input.length / 4
//for each rotation, find the max
var rotationIndex = 0
while (rotationIndex < rotatedSizes.size) {
var center = 0
var right = 0
var index = 1
var max = 0
var palindromeExistAtEnd = false
var originalIndex = 0
var maxStartIndex = 0
val palindromSizes = Array(paddedString.length) { 0 }
while (index < paddedString.length) {
//find the mirror of this index
val mirror = center - (index - center)
//if index is less than the right edge, pSize of this index is min of mirror or distance to right
if (index < right) {
palindromSizes[index] = min(palindromSizes[mirror], right - index)
}
//expand beyond the above pSize for this index
while ((index + palindromSizes[index]) < palindromSizes.size - 1
&& (index - palindromSizes[index] - 1) >= 0
&& paddedString[index + palindromSizes[index] + 1] == paddedString[index - palindromSizes[index] - 1]
) {
if (compressedSizes[index + palindromSizes[index] + 1] != compressedSizes[index - palindromSizes[index] - 1]) {
palindromSizes[index]++
break
} else {
palindromSizes[index]++
}
}
//move center and right of pSize moved beyond current right
if (index + palindromSizes[index] > right) {
center = index
right = palindromSizes[index]
}
var score = compressedSizes[index]
for (range in 1..palindromSizes[index]) {
score += (2 * Math.min(compressedSizes[index + range], compressedSizes[index - range]))
}
if (max < score) {
max = score
maxStartIndex = originalIndex - (max - compressedSizes[index]) / 2
}
if (compressedSizes[index] > 0) {
originalIndex += compressedSizes[index]
}
val endIndex = originalIndex + (score - compressedSizes[index]) / 2
if (!palindromeExistAtEnd && score > 1 && endIndex == inputLength) {
palindromeExistAtEnd = true
}
index++
}
rotatedSizes[rotationIndex] = max
if (rotationIndex < rotatedSizes.size - 1) {
var charsToRotate = 1
if (fold) {
val firstChar = paddedString[1]
val secondChar = if (inputLength > 1) {
if (compressedSizes[1] == 1)
firstChar else paddedString[1]
} else '.'
val lastChar = paddedString[paddedString.length - 2]
val secondLastChar = if (inputLength > 1) {
if (compressedSizes[paddedString.length - 2] == 1)
lastChar else paddedString[paddedString.length - 4]
} else '_'
if (palindromeExistAtEnd || firstChar == lastChar || firstChar == secondLastChar || secondChar == secondLastChar) {
charsToRotate = 1
} else if (maxStartIndex > 0) {
charsToRotate = maxStartIndex
for (skipIndex in 1..maxStartIndex) {
if (rotationIndex + skipIndex >= rotatedSizes.size) {
break
} else {
rotatedSizes[rotationIndex + skipIndex] = max
}
}
rotationIndex += maxStartIndex
if (rotationIndex >= rotatedSizes.size) {
break
}
compressedInput.clear()
compressedSizes.clear()
paddedString.clear()
var skipIndex = maxStartIndex
var skipTotal = 0
compressedSizes.add(0)
paddedString.append('#')
while (skipTotal < inputLength) {
var second = skipIndex + 1
while (skipTotal < skipTotal++ && input[skipIndex % inputLength] == input[second % inputLength]) {
second++
skipTotal++
}
compressedInput.add(input[skipIndex % inputLength])
paddedString.append(input[skipIndex % inputLength])
compressedSizes.add(second - skipIndex)
paddedString.append('#')
compressedSizes.add(0)
skipIndex = second
}
} else {
charsToRotate = 1
}
} else {
charsToRotate = 1
}
if (charsToRotate == 1) {
rotationIndex++
val firstChar = paddedString[1]
val lastChar = paddedString[paddedString.length - 2]
//if first is repeated, just reduce the count
if (compressedSizes[1] > 1) {
compressedSizes[1]--
} else {
//if not remove
compressedSizes.removeAt(0)
compressedSizes.removeAt(0)
//paddedString = StringBuilder(paddedString.removeRange(0, 2))
paddedString.delete(0, 2)
}
//if last is same as first, just increase count
if (lastChar == firstChar) {
compressedSizes[compressedSizes.size - 2]++
} else {
//if not add
compressedSizes.add(1)
compressedSizes.add(0)
paddedString.append(firstChar)
paddedString.append("#")
}
}
} else {
rotationIndex++
}
}
return rotatedSizes
}
/**
* Returns array of max palindromes after each rotation
*/
fun circularPalindromes(input: String): Array<Int> {
return getPalindromSizes(input)
}
fun main(args: Array<String>) {
val scan = Scanner(System.`in`)
val n = scan.nextLine().trim().toInt()
val s = scan.nextLine()
val result = circularPalindromes(s)
println(result.joinToString("\n"))
}