Skip to content

Commit 6d8e9b5

Browse files
committed
Prettify the code
1 parent 17d7c26 commit 6d8e9b5

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

strings/manacher.nim

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,41 @@ runnableExamples:
1717
doAssert(manacherLength(example1) == 5)
1818

1919
func manacherIndex*(s: string): HSlice[int, int] {.raises: [ValueError].} =
20-
## Find the start and stop index for the longest palindrome in a string by Manacher
21-
##
22-
## :returns: indexes i and j such that s\[i:j\] is the longest palindrome in s
23-
## :param s: string, lowercase ascii, no whitespace
24-
## :time complexity: O(len(s))
25-
## All the indexes refer to an intermediate string t
26-
## of the form "^#a#b#a#a#$" for s="abaa"
27-
if s.len == 0:
28-
raise newException(ValueError, "Empty string")
29-
let extraSymbols = toHashSet(['$', '^', '#'])
30-
let letters = toHashSet(s.toLowerAscii)
31-
assert disjoint(extraSymbols, letters) # Forbidden letters
32-
if s == "":
33-
return 0 .. 1
34-
let s = "^#" & join(s, "#") & "#$"
35-
var
36-
center = 1
37-
distance = 1
38-
p = repeat(0, len(s)) # Palindrome radii for each index in s
39-
for index in 2 ..< len(s)-1:
40-
# reflect index with respect to center
41-
let mirror = 2 * center - index # = center - (index - center)
42-
p[index] = max(0, min(distance - index, p[mirror]))
43-
# grow palindrome centered in i
44-
while s[index + 1 + p[index]] == s[index - 1 - p[index]]:
45-
p[index] += 1
46-
# adjust center if necessary
47-
if index + p[index] > distance:
48-
center = index
49-
distance = index + p[index]
50-
# find the argmax index in p
51-
var
52-
j = maxIndex(p)
53-
k = p[j]
54-
return (j - k) div 2 ..< (j + k) div 2 # extract solution
20+
## Find the start and stop index for the longest palindrome in a string by Manacher
21+
##
22+
## :returns: indexes i and j such that s\[i:j\] is the longest palindrome in s
23+
## :param s: string, lowercase ascii, no whitespace
24+
## :time complexity: O(len(s))
25+
## All the indexes refer to an intermediate string t
26+
## of the form "^#a#b#a#a#$" for s="abaa"
27+
if s.len == 0:
28+
raise newException(ValueError, "Empty string")
29+
let extraSymbols = toHashSet(['$', '^', '#'])
30+
let letters = toHashSet(s.toLowerAscii)
31+
assert disjoint(extraSymbols, letters) # Forbidden letters
32+
if s == "":
33+
return 0 .. 1
34+
let s = "^#" & join(s, "#") & "#$"
35+
var
36+
center = 1
37+
distance = 1
38+
p = repeat(0, len(s)) # Palindrome radii for each index in s
39+
for index in 2 ..< len(s)-1:
40+
# reflect index with respect to center
41+
let mirror = 2 * center - index # = center - (index - center)
42+
p[index] = max(0, min(distance - index, p[mirror]))
43+
# grow palindrome centered in i
44+
while s[index + 1 + p[index]] == s[index - 1 - p[index]]:
45+
p[index] += 1
46+
# adjust center if necessary
47+
if index + p[index] > distance:
48+
center = index
49+
distance = index + p[index]
50+
# find the argmax index in p
51+
var
52+
j = maxIndex(p)
53+
k = p[j]
54+
return (j - k) div 2 ..< (j + k) div 2 # extract solution
5555

5656
func manacherString*(s: string): string {.raises: [ValueError].} =
5757
## Returns the longest palindrome
@@ -81,7 +81,7 @@ when isMainModule:
8181
check manacherIndex("telet") == 0 .. 4
8282
check manacherLength("telet") == 5
8383
check manacherString("telet") == "telet"
84-
84+
8585
test "Empty string":
8686
doAssertRaises(ValueError):
8787
discard manacherIndex("")

0 commit comments

Comments
 (0)