-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathsaving_the_universe_again.py
More file actions
58 lines (48 loc) · 1.2 KB
/
saving_the_universe_again.py
File metadata and controls
58 lines (48 loc) · 1.2 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
def calculate_damage(p, d):
damage = 1
total = 0
for i in range(len(p)):
if p[i] == 'C':
damage *= 2
else:
total += damage
return total <= d
def is_impossible(p, d):
counter = 0
for c in p:
if c == 'S':
counter += 1
return counter > d
def swap(string, i, j):
lst = list(string)
lst[i], lst[j] = lst[j], lst[i]
return "".join(lst)
def solve(p, d):
swap_count = 0
i = len(p) - 1
if calculate_damage(p, d):
return swap_count
if is_impossible(p, d):
return 'IMPOSSIBLE'
while i >= 0:
while p[i] == 'C' and i >= 0:
i -= 1
while p[i] == 'S' and i >= 0:
i -= 1
j = i
while True:
if j == len(p) - 1 or p[j + 1] == 'C':
break
p = swap(p, j, j + 1)
j += 1
swap_count += 1
if calculate_damage(p, d):
return swap_count
def read_input():
ncases = int(input())
for case in range(1, ncases + 1):
d, p = input().split(" ")
d = int(d)
moves = solve(p, d)
print("CASE #{}: {}".format(case, moves))
read_input()