-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (61 loc) · 1.88 KB
/
main.py
File metadata and controls
67 lines (61 loc) · 1.88 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
def leven_length(word1, word2):
table = []
for i in range(len(word1)+1):
table.append([])
for j in range(len(word2)+1):
table[i].append(0)
for i in range(1, len(word1)+1):
table[i][0] = i
for j in range(1, len(word2)+1):
table[0][j] = j
for i in range(1, len(word1)+1):
for j in range(1, len(word2)+1):
var = 0
if word1[i-1] != word2[j-1]:
var = 1
table[i][j] = min(min(table[i][j-1]+1, table[i-1][j]+1), table[i-1][j-1]+var)
return table[len(word1)][len(word2)]
words = []
k = int(input())
border = int(input())
kk = k
while k:
word = input()
words.append(word)
k -= 1
clusters = {}
active = []
for i in range(len(words)):
clusters[i] = {i}
active.append(1)
dp = []
for i in range(len(words)):
dp.append([])
for j in range(len(words)):
dp[i].append(-1)
for i in range(len(words)):
for j in range(len(words)):
dp[i][j] = leven_length(words[i], words[j])
for q in range(10):
lens = []
for keyi, vali in clusters.items():
for keyj, valj in clusters.items():
if active[keyi] and active[keyj] and keyi != keyj:
lens.append((dp[keyi][keyj], (keyi, keyj)))
used = []
for i in range(kk):
used.append(0)
lens.sort()
for pair in lens:
first = pair[1][0]
second = pair[1][1]
if not used[first] and not used[second] and not first == second and pair[0] <= border:
used[first] = 1
used[second] = 1
clusters[first] = set.union(clusters[first], clusters[second])
clusters.pop(second, None)
for i in range(len(clusters)):
if not i == first and not i == second:
dp[i][first] = (dp[i][first] + dp[i][second])/2
dp[first][i] = dp[i][first]
print(clusters)