Problem:
Problem B: Step Step Evolution Japanese video game company has developed the music video game called Step Step Evolution.
The gameplay of Step Step Evolution is very simple. Players stand on the dance platform, and
step on panels on it according to a sequence of arrows shown in the front screen. There are eight types of direction arrows in the Step Step Evolution: UP, UPPER RIGHT,
RIGHT, LOWER RIGHT, DOWN, LOWER LEFT, LEFT, and UPPER LEFT. These direction
arrows will scroll upward from the bottom of the screen. When the direction arrow overlaps the
stationary arrow nearby the top, then the player must step on the corresponding arrow panel
on the dance platform. The figure below shows how the dance platform looks like.   Figure 1: the dance platform for Step Step Evolution  In the game, you have to obey the following rule:  Except for the beginning of the play, you must not press the arrow panel which is not
  correspond to the edit data.  The foot must stay upon the panel where it presses last time, and will not be moved until
  it’s going to press the next arrow panel.  The left foot must not step on the panel which locates at right to the panel on which the
right foot rests. Conversely, the right foot must not step on the panel which locates at left
to the panel on which the left foot rests.  As for the third condition, the following figure shows the examples of the valid and invalid
footsteps.   Figure 2: the examples of valid and invalid footsteps  The first one (left foot for LEFT, right foot for DOWN) and the second one (left foot for LOWER
LEFT, right foot for UPPER LEFT) are valid footstep. The last one (left foot for RIGHT, right
foot for DOWN) is invalid footstep. Note that, at the beginning of the play, the left foot and right foot can be placed anywhere at
the arrow panel. Also, you can start first step with left foot or right foot, whichever you want. To play this game in a beautiful way, the play style called “ Natural footstep style” is commonly
known among talented players. “Natural footstep style” is the style in which players make steps
by the left foot and the right foot in turn. However, when a sequence of arrows is difficult,
players are sometimes forced to violate this style. Now, your friend has created an edit data (the original sequence of direction arrows to be pushed)
for you. You are interested in how many times you have to violate “Natural footstep style” when
you optimally played with this edit data. In other words, what is the minimum number of times
you have to step on two consecutive arrows using the same foot? Input The input consists of several detasets. Each dataset is specified by one line containing a sequence
of direction arrows. Directions are described by numbers from 1 to 9, except for 5. The figure
below shows the correspondence between numbers and directions. You may assume that the length of the sequence is between 1 and 100000, inclusive. Also, arrows
of the same direction won’t appear consecutively in the line. The end of the input is indicated
by a single “ # ”.   Figure 3: the correspondence of the numbers to the direction arrows  Output For each dataset, output how many times you have to violate “Natural footstep style” when you
play optimally in one line. Sample Input 1
12
123
369
6748
4247219123
1232123212321232
# Output for the Sample Input 0
0
1
0
1
2
7

Solution (Python):
pos = [0,1,2,0,-1,2,0,1,2]
while 1:
    s = raw_input()
    if s == "
    ans = 10**9
    for ini in range(2):
        tmp = 0
        b = int(s[0])-1
        lr = ini
        for i in range(1,len(s)):
            f = int(s[i])-1
            if lr == 0:
                if pos[b] < pos[f]: tmp += 1
                else: lr = 1-lr
            else:
                if pos[b] > pos[f]: tmp += 1
                else: lr = 1-lr
            b = f
        ans = min(ans, tmp)
    print ans

Solution (Python):
pos = [0,1,2,0,-1,2,0,1,2]
while 1:
    s = raw_input()
    if s == "
    ans = 10**9
    for ini in range(2):
        tmp = 0
        b = pos[int(s[0])-1]
        lr = ini
        for i in s[1:]:
            f = pos[int(i)-1]
            if [b<f, b>f][lr]: tmp += 1
            else: lr = 1-lr
            b = f
        ans = min(ans, tmp)
    print ans

Solution (Python):
while 1:
    s = raw_input()
    if s == "
    ans = 10**9
    for ini in range(2):
        tmp = 0
        b = (int(s[0])-1)%3
        lr = ini
        for i in s[1:]:
            f = (int(i)-1)%3
            if [b<f, b>f][lr]: tmp += 1
            else: lr = 1-lr
            b = f
        ans = min(ans, tmp)
    print ans

Solution (Python):
def step_shift(foot, next):
	if (not(foot[0]) and foot[1] > next) or (foot[0] and foot[1] < next):
		foot[2] += 1
	else:
		foot[0]^= 1
	foot[1] = next
	return foot

while 1:
	step = raw_input()
	if step == "
	step = [int(s) for s in step]
	s_loc = {1:-1, 4:-1, 7:-1, 2:0, 8:0, 3:1, 6:1, 9:1} 
	foot_l = [False, s_loc[step[0]], 0]
	foot_r = [True, s_loc[step[0]], 0]
	for s in step[1:]:
		next = s_loc[s]
		foot_l = step_shift(foot_l, next)
		foot_r = step_shift(foot_r, next)
	print min(foot_l[2], foot_r[2])

Solution (Python):
while 1:
    s = raw_input()
    if s == "
    l = len(s)
    ps = [(int(c)-1) % 3 for c in s]
    dpl = [1e9]*l; dpr = [1e9]*l
    dpl[0] = dpr[0] = 0
    for i in xrange(1, l):
        if ps[i-1] <= ps[i]:
            dpl[i] = dpr[i-1]
        if ps[i] <= ps[i-1]:
            dpr[i] = dpl[i-1]
        dpl[i] = min(dpl[i], dpl[i-1]+1)
        dpr[i] = min(dpr[i], dpr[i-1]+1)
    print min(dpl[l-1], dpr[l-1])

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        s = S()
        if s == '
            break

        l = True
        lr = 0
        for i in range(1,len(s)):
            c = s[i]
            p = s[i-1]
            cl = int(c) % 3
            if cl == 0:
                cl = 3
            pl = int(p) % 3
            if pl == 0:
                pl = 3

            if l and cl > pl:
                lr += 1
            elif not l and cl < pl:
                lr += 1
            else:
                l = not l
        l = False
        tr = 0
        for i in range(1,len(s)):
            c = s[i]
            p = s[i-1]
            cl = int(c) % 3
            if cl == 0:
                cl = 3
            pl = int(p) % 3
            if pl == 0:
                pl = 3

            if l and cl > pl:
                tr += 1
            elif not l and cl < pl:
                tr += 1
            else:
                l = not l

        rr.append(min(lr,tr))

    return '\n'.join(map(str, rr))


print(main())

Solution (Python):

def main():
    while True:
        line=raw_input()
        if line=="
        solve(line)
        
def solve(line):
    length=len(line)
    INF=1<<29
    dp=[[[[INF for l in range(3)] for k in range(3)] for j in range(2)] for i in range(2)]
    dp[1][1][0][2]=0
    dp[1][0][0][2]=0
    for i in range(length):
        n=i%2
        n2=(i+1)%2
        pos=(int(line[i])+2)%3
        for l in range(3):
            for r in range(3):
                if l<=pos:
                    dp[n][1][l][pos]=min(dp[n][1][l][pos], min(dp[n2][0][l][r], dp[n2][1][l][r]+1) )
                if pos<=r:
                    dp[n][0][pos][r]=min(dp[n][0][pos][r], min(dp[n2][1][l][r], dp[n2][0][l][r]+1) )
        for l in range(3):
            for r in range(3):
                for j in range(2):
                    dp[n2][j][l][r]=INF
    ans=INF
    tmp=(length-1)%2
    for j in range(2):
        for l in range(3):
            for r in range(3):
                ans=min(ans, dp[tmp][j][l][r])
    print ans

if __name__=="__main__":    main()

Solution (Python):
import sys

for line in sys.stdin:
    if line[0] == '
        break
    line = line.strip()
    step = [0, 0]
    before = int(line[0]) % 3 if int(line[0]) % 3 != 0 else 3
    NULL = 1000000
    for c in line[1:]:
        n = int(c) % 3 if int(c) % 3 != 0 else 3
        if n > before:

            step[0], step[1] = NULL, min(step[0], step[1] + 1)
        elif n < before:
            step[0], step[1] = min(step[0] + 1, step[1]), NULL
        else:
            step[0], step[1] = step[1], step[0]
        before = n

    print min(step)