forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0943.py
47 lines (40 loc) · 1.68 KB
/
0943.py
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
class Solution:
def shortestSuperstring(self, A: List[str]) -> str:
n = len(A)
g = [[0] * n for _ in range(n)]
path = [[0] * n for _ in range(1 << n)]
vis = (1 << n) - 1
def dis(a, b):
for i in range(len(a)):
if len(a) - i <= len(b) and a[i:] == b[:len(a) - i]:
return len(b) - len(a) + i
return len(b)
for i in range(n):
for j in range(n):
if i == j: continue
g[i][j] = dis(A[i], A[j]) #计算字符串距离
dp = [[float("inf")] * n for _ in range(1 << n)]
best, pos = float("inf"), -1 #记录最优解的位置
# TSP
for i in range(1 << n):
for j in range(n):
if ((i >> j) & 1) == 0: continue
pre = i - (1 << j)
if pre == 0: # j作为第一个位置开始
dp[i][j] = len(A[j])
else:
for k in range(n):
if ((pre >> k) & 1) == 0: continue
p = dp[pre][k] + g[k][j] # 如果pre>k + k->j < pre->j,进行放缩
if dp[i][j] > p:
dp[i][j], path[i][j] = p, k # path记录j之前的位置k
if i == vis and best > dp[i][j]: # 寻找最优解
best, pos = dp[i][j], j
res, t = A[pos], vis
while 1:
pre = path[t][pos] # 依次弹出pos的前一个位置
t -= (1 << pos)
if t == 0: break
res = A[pre] + res[len(A[pos])-g[pre][pos]:]
pos = pre
return res