-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsofteer.py
More file actions
43 lines (32 loc) · 858 Bytes
/
softeer.py
File metadata and controls
43 lines (32 loc) · 858 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
37
38
39
40
41
42
43
def is_right(string):
cnt = 1
alp = string[0]
for i in range(1, n):
if string[i] != alp:
if cnt < k:
return False
cnt = 1
alp = string[i]
else:
cnt += 1
if cnt < k:
return False
return True
def solution(string, changed = 0):
if is_right(string):
return 0
if string in dp:
return dp[string]
temp = 2000
for i in range(n):
for d in range(-1, 2, 2):
ni = i + d
if 0 <= ni < n and not changed & 1 << i and string[i] != string[ni]:
temp = min(temp, solution(string[:i] + string[ni] + string[i+1:], changed | 1 << i) + 1)
dp[string] = temp
return temp
n, k = map(int, input().split())
S = input()
dp = {}
print(solution(S))
print(dp)