forked from anubhab91/CodeForces-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNightAtTheMuseum.py
More file actions
33 lines (22 loc) · 997 Bytes
/
NightAtTheMuseum.py
File metadata and controls
33 lines (22 loc) · 997 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
__author__ = 'Devesh Bajpai'
'''
https://codeforces.com/problemset/problem/731/A
Solution: Iterate over the string and for every pair of adjacent characters, check if the forward or backward move
is optimal. For forward, we can take the difference of ascii values of previous and current character. Since we want
to know the moves, we take the difference as absolute. For the backward move, we subtract the forward move from 26, since
we have 26 characters only. Minimum of these moves is added to the total moves.
In order to take care of the first character's move, we prepend 'a' to the string.
'''
def solve(str):
str = 'a' + str
totalMoves = 0
for i in xrange(1, len(str)):
prevChar = str[i-1]
curChar = str[i]
forwardMove = abs(ord(prevChar) - ord(curChar))
backwardMove = 26 - forwardMove
totalMoves += min(forwardMove, backwardMove)
return totalMoves
if __name__ == "__main__":
str = raw_input()
print solve(str)