Codejam Qualification Round 2018. Task 1
Python Only
def damage(SC):
K,D,N=1,0,len(SC)
if 'C' not in SC:
D==N
for i in range(N):
if SC[i]=='S':
D+=K
else:
K*=2
return D
def reduce_damage(SC,D):
C,N=0,len(SC)
if damage(SC) > D:
while 'CS' in SC:
idx=SC[::-1].index('SC')
SC=SC[:N-idx-2]+'SC'+SC[N-idx:]
C+=1
if damage(SC) <= D:
break
return C,damage(SC)-D
T=int(input())
for t in range(T):
[D,SC]=input().split()
D=int(D)
# print(D,damage(SC))
# print(reduce_damage(SC,D))
C,DIFF=reduce_damage(SC,D)
if DIFF > 0:
print('Case #{}: IMPOSSIBLE'.format(t+1))
else:
print('Case #{}: {}'.format(t+1,C))