-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManacher.py
More file actions
36 lines (31 loc) · 925 Bytes
/
Manacher.py
File metadata and controls
36 lines (31 loc) · 925 Bytes
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
# 回文子串数量O(n),待简化
def countSubstrings(s: str) -> int:
def manacher():
# 马拉车算法
arm = [0] * n
x, y = 0, -1
for i in range(0, n):
k = 1 if i > y else min(arm[x + y - i], y - i + 1)
# 持续增加回文串的长度
while 0 <= i - k and i + k < n and s[i - k] == s[i + k]:
k += 1
arm[i] = k
# 更新右侧最远的回文串边界
k -= 1
if i + k > y:
x = i - k
y = i + k
# 返回每个位置往右的臂长
return arm
s = "#" + "#".join(list(s)) + "#"
n = len(s)
dp = manacher()
# 计算真实长度回文串的个数
ans = 0
for num in dp:
num = (num * 2 - 1) // 2
if num % 2 == 0:
ans += num // 2
else:
ans += num // 2 + 1
return ans