Problem:
Baseball Simulation Ichiro likes baseball and has decided to write a program which simulates baseball. The program reads events in an inning and prints score in that inning. There are only three events as follows: Single hit  put a runner on the first base.  the runner in the first base advances to the second base and the runner in the second base advances to the third base.  the runner in the third base advances to the home base (and go out of base) and a point is added to the score.  Home run  all the runners on base advance to the home base.  points are added to the score by an amount equal to the number of the runners plus one.  Out  The number of outs is increased by 1.  The runners and the score remain stationary.  The inning ends with three-out.  Ichiro decided to represent these events using "HIT", "HOMERUN" and "OUT", respectively. Write a program which reads events in an inning and prints score in that inning. You can assume that the number of events is less than or equal to 100. Input The input consists of several datasets. In the first line, the number of datasets n is given. Each dataset consists of a list of events (strings) in an inning. Output For each dataset, prints the score in the corresponding inning. Sample Input 2
HIT
OUT
HOMERUN
HIT
HIT
HOMERUN
HIT
OUT
HIT
HIT
HIT
HIT
OUT
HIT
HIT
OUT
HIT
OUT
OUT Output for the Sample Input 7
0

Solution (Python):
var1 = int(input())
for m in range(0, var1):
    OutCount = 0
    BaseCount = 0
    PointCount = 0
    for i in range(0, 100):
        try:
            str1 = input()
            if str1 == 'HIT':
                BaseCount = BaseCount + 1
                if BaseCount == 4:
                    PointCount = PointCount + 1
                    BaseCount = 3
            elif str1 == 'HOMERUN':
                PointCount = PointCount + BaseCount + 1
                BaseCount = 0
            elif str1 == 'OUT':
                OutCount = OutCount + 1
                if OutCount == 3:
                    print(PointCount)
                    break
        except:
            break

Solution (Python):
n = int(raw_input())
   
runner = 0
run = 0
out = 0
inning = 0
while inning < n:
    event = raw_input()
    if event == 'HIT':
        if runner == 3:
            run += 1
        else:
            runner += 1
    elif event == 'HOMERUN':
        run += (runner+1)
        runner = 0
    else:
        if out == 2:
            print run
            runner = 0
            run = 0
            out = 0
            inning += 1
        else:
            out += 1

Solution (Python):
n=input()
for _ in [0]*n:
  i=0
  B=[0]*3
  c=0
  while i<3:
    s=raw_input()[1]
    if s=='I':
      if B[2]==1:c+=1
      B=[1]+B[0:2]
    elif s=='O':
      c+=sum(B)+1
      B=[0]*3
    elif s=='U':i+=1
  print c

Solution (Python):
n=input()
for _ in [0]*n:
  i=0
  b=0
  c=0
  while i<3:
    s=raw_input()[1]
    if s=='I':
      if b==3:c+=1
      else: b+=1
    elif s=='O':
      c+=b+1
      b=0
    elif s=='U':i+=1
  print c

Solution (Python):
n=input()
for _ in [0]*n:
  i,b,c=0,0,0
  while i<3:
    s="IOU".index(raw_input()[1])
    if s==0:
      if b==3: c+=1
      else: b+=1
    elif s==1:
      c+=b+1
      b=0
    elif s==2: i+=1
  print c

Solution (Python):
import sys

import sys
N = int(input())
turn = 0
point = 0
out = 0
base = [0, 0, 0]

point_list = []
for idx, line in enumerate(sys.stdin):
    if line == 'HOMERUN\n':
        
        cnt = 1
        if base[0] == 1:
            cnt += 1
        if base[1] == 1:
            cnt += 1
        if base[2] == 1:
            cnt += 1
        point += cnt
        
        base = [0, 0, 0]

    if line == 'HIT\n':
        
        if base[2] == 1:
            base[2] = 0
            point += 1
            
        if base[1] == 1:
            base[2] = 1
        if base[0] == 1:
            base[1] = 1
        if base[0] == 0:
            base[0] = 1
        

    if line == 'OUT\n':
        
        out += 1
        
        if out == 3:
            
            point_list.append(point)
            turn += 1
            point = 0
            base = [0, 0, 0]
            out = 0
            if turn == N:
                
                break
            continue

for e in point_list:
    print(e)

Solution (Python):


n = int(input())

def solve():
    o = 0
    s = 0
    b = [False, False, False]
    while o < 3:
        inst = input()
        if inst == 'OUT':
            o += 1
        elif inst == 'HIT':
            if b[2]:
                s += 1
            b2 = [True]
            b2.extend(b[0:2])
            b = b2
        elif inst == 'HOMERUN':
            s += 1
            for k in range(3):
                if b[k]:
                    b[k] = False
                    s += 1
    return s

for _ in range(n):
    print(solve())

Solution (Python):



def main():
    
    N = int(raw_input())

    for i in range(N):
        data = {}
        b = Baseball()
        while(b.out < 3):
            event = raw_input()
            b.decisionScore(event)

        print(b.score)

class Baseball():
    def __init__(self):
        self.out = 0
        self.score = 0
        self.que = [0,0,0]  

    def decisionScore(self, event):
        if event == "OUT":
            self.out += 1
        elif event == "HIT":
            self.score += self.addRunner()
        elif event == "HOMERUN":
            self.score += self.clearRunnner() + 1
        else:
            print("Wrong data")

    def addRunner(self):
        self.que.append(1)
        point = self.que[0]
        self.que = self.que[1:]
        return point

    def clearRunnner(self):
        point = sum(self.que)
        self.que = [0,0,0]
        return point

if __name__=='__main__':
    main()

Solution (Python):
SCORE=[]
preSCORE=0
OUTcount=0
BASE=[False]*3
n=int(input())
for i in range(n):
    while OUTcount<3:
        spam=input()
        if 'HIT' in spam:
            if BASE[2]==True:
                preSCORE+=1
            BASE[2]=BASE[1]
            BASE[1]=BASE[0]
            BASE[0]=True
        elif 'OUT' in spam:
            OUTcount+=1
        elif 'HOMERUN' in spam:
            preSCORE+=sum(x for x in BASE)+1
            BASE=[False]*3
    SCORE.append(preSCORE)
    BASE=[False]*3
    OUTcount=0
    preSCORE=0
for i in SCORE:
    print(i)

Solution (Python):
SCORE=[]
n=int(input())
for i in range(n):
    BASE=[False]*3
    OUTcount=0
    preSCORE=0
    while OUTcount<3:
        spam=input()
        if 'HIT' in spam:
            if BASE[2]==True:
                preSCORE+=1
            BASE[2]=BASE[1]
            BASE[1]=BASE[0]
            BASE[0]=True
        elif 'OUT' in spam:
            OUTcount+=1
        elif 'HOMERUN' in spam:
            preSCORE+=sum(x for x in BASE)+1
            BASE=[False]*3
    SCORE.append(preSCORE)
for i in SCORE:
    print(i)

Solution (Python):






for _ in range(int(input())):
    ans = 0
    out = 0
    hit = 0
    while True:
        w = input()
        if w == "OUT":
            out += 1
            if out == 3:
                break
        elif w == "HIT":
            if hit == 3:
                ans += 1
            else:
                hit += 1
        elif w == "HOMERUN":
            ans += hit + 1
            hit = 0
    print(ans)

Solution (Python):
n=int(raw_input())
game=0
while game<n:
    run=0
    score=0
    out=0
    while out<3:
        ev=raw_input()
        if ev=='OUT':
            out=out+1
        elif ev=='HIT':
            run=run+1
            if run>3:
                run=3
                score=score+1
        else:
            score=score+run+1
            run=0
    print score
    game=game+1

Solution (Python):
while True:
    try:
        n = int(raw_input())
    except EOFError:
        break

    for i in range(n):
        base = [False,False,False] 
        point = 0
        out = 0
        while out <3:
            order = raw_input()
            if order =='HIT':
                base.insert(0,True)
                if base[-1]:
                    base.pop()
                    point += 1
                else:
                    base.pop()
            elif order == 'OUT':
                out += 1
            elif order == 'HOMERUN':
                point += sum(base) + 1
                base = [False,False,False]
        else:
            print point

Solution (Python):
n = input()
base, out, score = [0]*3, 0, 0
while True:
    if n == 0:
        break
    event = raw_input()
    if event == 'OUT':
        out += 1
        if out == 3:
            print score
            base, out, score = [0]*3, 0, 0
            n -= 1
    elif event == 'HIT':
            score += base.pop()
            base.insert(0, 1)
    else:
            score += sum(base)+1
            base = [0] * 3

Solution (Python):
from collections import deque

inning = 0
point = 0
out = 0
base = deque([0] * 3)

n = input()
while True:
    event = raw_input()
    if event == "HIT":
        point += base.pop()
        base.appendleft(1)
    elif event == "HOMERUN":
        point += base.count(1) + 1
        base = deque([0] * 3)
    else:
        out += 1
        if out == 3:
            print point
            point = 0
            out = 0
            base = deque([0] * 3)
            inning += 1
            if inning == n: break

Solution (Python):
N = int(raw_input())
p = 0
b = [0,0,0]
o = 0
while True:
    try:
        e = raw_input()
        if e == 'HIT':
            if b[-1] == 1:
                p += 1
            b[0],b[1],b[2] = 1,b[0],b[1]
        elif e == 'HOMERUN':
            p += sum(b)+1
            b = [0,0,0]
        elif e == 'OUT':
            o += 1
            if o == 3:
                print p
                p = 0
                b = [0,0,0]
                o = 0
    except EOFError:
        break

Solution (Python):
N = int(input())

base = [0] * 3
out = 0
point = 0
inning = 0

while True:
    event = input()

    if event == "HIT":
        if base[2] == 1:
            point += 1
        base[2] = base[1]
        base[1] = base[0]
        base[0] = 1

    elif event == "HOMERUN":
        point += sum(base) + 1
        base = [0] * 3

    elif event == "OUT":
        out += 1
        if out == 3:
            print(point)
            inning += 1
            if inning == N:
                break
            base = [0] * 3
            out = 0
            point = 0

Solution (Python):
n = int(input())

for i in range(n):
    first = False
    second = False
    third = False
    out = 0
    points = 0
    
    while (out != 3):
        event = raw_input()
        if event == "HIT":
            if first == True:
                if second == True:
                    if third == True:
                        points += 1

                    else:
                        third = True
                else:
                    second = True
            else:
                first = True

        elif event == "HOMERUN":
            if third == True:
                points += 4
            elif second == True:
                points += 3
            elif first == True:
                points += 2
            else:
                points += 1

            first = second = third = False

        else:
            out += 1

    print points

Solution (Python):
n = int(input())
for i in range(n):
  l = [1,0,0,0]
  c = 0
  o_c = 0
  while True:
    s = input()
    if s == 'HIT':
      if l[3] == 1:
        c += 1
      for i in range(4):
        if i == 3:
          l[3-i] == 1
        else:
          l[3-i] = l[2-i]
    elif s == 'HOMERUN':
      c += sum(l)
      l = [1,0,0,0]
    else:
      o_c += 1
      if o_c == 3:
        print(c)
        break

Solution (Python):
for i in range(int(input())):
    point = 0
    out = 0
    runner = [0, 0, 0]
    while True:
        event = input()
        if event == 'HIT':
            if runner[2] == 1:
                point += 1
                runner[2] = 0
            if runner[1] == 1:
                runner[2] = 1
                runner[1] = 0
            if runner[0] == 1:
                runner[1] = 1
                runner[0] = 0
            runner[0] = 1
        elif event == 'HOMERUN':
            point += (sum(runner)+1)
            runner = [0, 0, 0]
        elif event == 'OUT':
            out += 1
            if out == 3:
                print(point)
                break

Solution (Python):
for i in range(int(input())):
    point = 0
    out = 0
    runner = [0, 0, 0]
    while True:
        event = input()
        if event == 'HIT':
            if runner[2] == 1:
                point += 1
                runner[2] = 0
            if runner[1] == 1:
                runner[2] = 1
                runner[1] = 0
            if runner[0] == 1:
                runner[1] = 1
            runner[0] = 1
        elif event == 'HOMERUN':
            point += (sum(runner)+1)
            runner = [0, 0, 0]
        elif event == 'OUT':
            out += 1
            if out == 3:
                print(point)
                break

Solution (Python):
def hit(point, base, out_count):
    new_base = (base << 1) + 1
    return point + (new_base > 0b111), new_base & 0b111, out_count


def homerun(point, base, out_count):
    return point + (bin(base & 0b111).count('1') + 1), 0, out_count


def out(point, base, out_count):
    return point, base, out_count - 1


F = {'HIT': hit, 'HOMERUN': homerun, 'OUT': out}

i, point, base, out_count = int(input()), 0, 0, 3

while i:

    point, base, out_count = F[input().strip()](point, base, out_count)

    if not out_count:
        i -= 1
        print(point)
        point, base, out_count = 0, 0, 3

Solution (Python):
import sys

n = input()
events = []
for line in sys.stdin:
    events.append(line.strip())

out = 0
score = 0
runner = 0
for event in events:
    if event == 'OUT':
        out += 1
        if out == 3:
            print score
            out = 0
            score = 0
            runner = 0
    elif event == 'HIT':
        if runner == 3:
            score += 1
        else:
            runner += 1
    elif event == 'HOMERUN':
        score += runner+1
        runner = 0

Solution (Python):
def game():
    score=out=0
    runner=[0]*3
    while 1:
        t=raw_input()
        if t=='OUT':
            out+=1
            if out==3:
                return score
        elif t=='HOMERUN':
            score+=(1+runner.count(1))
            runner=[0]*3
        elif t=='HIT':
            if runner[2]==1:
                score+=1
                runner[2]=0
            runner=[1,runner[0],runner[1]]

n=int(raw_input())
for i in range(n):
    print game()

Solution (Python):
n = int(input())
score = [0 for i in range(n)]
inning = 1
runner = [0 for i in range(3)]
outCount = 0
inningScore = 0
while inning <= n:
    str = input()
    if str == "OUT":
        outCount += 1
    elif str == "HIT":
        runner.insert(0, 1)
        inningScore += runner[3]
        runner.pop()
    elif str == "HOMERUN":
        inningScore += 1
        for i in range(len(runner)):
            inningScore += runner[i]
            runner[i] = 0
    if outCount == 3:
        for i in range(len(runner)):
            runner[i] = 0
        score[inning-1] = inningScore
        inningScore = 0
        outCount = 0
        inning += 1
for i in range(len(score)):
    print(score[i])

Solution (Python):
def init_state():
    return 0, 0, 0


def main():
    n = int(input())

    point, hit, out = init_state()

    while True:
        if n == 0:
            break

        event = input()

        if event == 'HIT':
            if hit >= 3:
                point += 1
            else:
                hit += 1
        elif event == 'OUT':
            out += 1
        elif event == 'HOMERUN':
            point += hit + 1
            hit = 0

        if out == 3:
            print(point)
            point, hit, out = init_state()
            n -= 1


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
score = 0
outcount = 0
first = 0
second = 0
third = 0
for i in range(n):
	while outcount < 3:
		event = input()
		if event == 'HIT':
			if third == 1:
				score += 1
			third = second
			second = first
			first = 1
		if event == 'HOMERUN':
			score = score + first + second + third + 1
			first = second = third = 0
		if event == 'OUT':
			outcount += 1
	print(score)
	score = 0
	outcount = 0
	first = 0
	second = 0
	third = 0

Solution (Python):
n = int(input())
for i in range(n):
    out_count = 0
    score = 0
    runner = []
    while True:
        if out_count == 3:
            print(score)
            break
            
        events = input()
        if events == "HIT":
            if len(runner) == 3:
                score += 1
            else:
                runner.append(1)

        elif events == "HOMERUN":
            score += len(runner) + 1
            runner = []

        elif events == "OUT":
            out_count += 1

Solution (Python):
n = int(input())
for i in range(n):
    r = 0
    p = 0
    o = 0
    while o < 3:
        e = input()
        if e == "OUT":
            o += 1
        elif e == "HIT":
            if r == 3:
                p += 1
            else:
                r += 1
        else:
            p += r + 1
            r = 0
    print(p)

Solution (Python):
for Panzerlied in range(0,int(input())):
  runners = 0
  outs = 0
  points = 0
  while outs<3:
    command=input()
    if command == "HIT":
      runners+=1
      if runners > 3:
        runners=3
        points+=1
    elif command == "HOMERUN":
      points+=(runners+1)
      runners=0
    else: 
      outs+=1
  print(points)

Solution (Python):
n = int(input())
for i in range(n):
    hit_cou = 0
    out_cou = 0
    poi = 0
    while True:     
        eve = input()
        if eve == "HIT":
            if hit_cou == 3:
                poi += 1
            else:
            
                hit_cou += 1
        elif eve == "OUT":
            if out_cou == 2:
                print(poi)
                break
            else:
                out_cou += 1
        elif eve == "HOMERUN":
            poi += hit_cou + 1
            hit_cou = 0

Solution (Python):
n=int(input())
b=[0]*3;s=o=0
while n:
    e=input()
    if len(e)>3:s+=sum(b)+1;b=[0]*3
    elif e[0]=='H':
        if b.pop():s+=1
        b=[1]+b
    else:
        o+=1
        if o==3:n-=1;b=[0]*3;print(s);o=s=0

Solution (Python):
n = int(input())

for _ in range(n):
    runners = []
    outs = 0
    score = 0
    while True:
        s = input()
        if s == "HIT":
            runners.append(0)
            for i in range(len(runners)):
                runners[i] += 1
                if runners[i] == 4:
                    score += 1
            if 4 in runners:
                runners.remove(4)
        elif s == "HOMERUN":
            score += len(runners) + 1
            runners = []
        elif s == "OUT":
            outs += 1
            if outs == 3:
                break
    print(score)

Solution (Python):
def main():
    kai, wo = 0, []
    kai = input()
    while(True):
        try:
            wo.append(raw_input())
        except EOFError:
            break
    R, S, O = 0, 0, 0
    for i in wo:
        if i == "HIT":
            if R < 3:
                R += 1
            else:
                S += 1

        elif i == "OUT":
            O += 1
            if O >= 3:
                print S
                R, S, O = 0, 0, 0
            else: pass
        elif i == "HOMERUN":
            R += 1
            S += R
            R = 0



if __name__=="__main__":
    main()

Solution (Python):
N = int(input())
for i in range(N):
    out = 0
    hit = 0
    point = 0
    
    while out < 3 :
         a = input()
         if a == "HIT":
              if hit < 3:
                   hit = hit + 1
              else:
                  point = point + 1
         elif a == "HOMERUN":
            point += hit + 1
            hit = 0
         elif a == "OUT":
            out += 1
    print(point)

Solution (Python):


import sys
import os

n = int(input())
for i in range(n):
    base = [0, 0, 0]
    score = 0
    out_count = 0
    while True:
        s = input().strip()
        if s == 'HIT':
            if base[2] == 1:
                score += 1
            base[2] = base[1]
            base[1] = base[0]
            base[0] = 1

        elif s == 'HOMERUN':
            score += sum(base) + 1
            base = [0, 0, 0]

        elif s == 'OUT':
            out_count += 1
            if out_count > 2:
                break
    print(score)

Solution (Python):
n = int(input())
out_count = 0
score = 0
runners = [0, 0, 0]

while n:
    event = input()
    if event == "HIT":
        if runners.pop():
            score+=1
        runners = [1] + runners
    if event == "HOMERUN":
        score += 1 + sum(runners)
        runners = [0, 0, 0]
                
    if event == "OUT":
        out_count += 1
        if out_count > 2:
            n-=1
            print(score)
            score = 0
            out_count = 0
            runners = [0, 0, 0]

Solution (Python):
n = int(input())
score = [0] * n
for i in range(n):
    out_count = 0
    runner = 0
    while True:
        data = input()
        if data == "OUT":
            out_count += 1
            if out_count == 3:
                break
        elif data == "HIT":
            runner += 1
            if runner == 4:
                runner = 3
                score[i] += 1
        else:
            score[i] += runner + 1
            runner = 0
print(*score, sep="\n")

Solution (Python):
import sys
n = int(sys.stdin.readline().rstrip())
for i in range(n):
    out = score = 0
    runner = [0] * 3
    while out < 3:
        st = sys.stdin.readline().rstrip()
        if st == 'HIT':
            score += runner[2]
            runner = [1] + runner[:-1]
        elif st == 'HOMERUN':
            score += sum(runner) + 1
            runner = [0] * 3
        else:
            out += 1
    print(score)

Solution (Python):
import sys
n = int(sys.stdin.readline().rstrip())
for i in range(n):
    out = score = 0
    runner = [0] * 3
    while out < 3:
        st = sys.stdin.readline().rstrip()
        if st == 'HIT':
            score += runner[2]
            runner = [1] + runner[:-1]
        elif st == 'HOMERUN':
            score += sum(runner) + 1
            runner = [0] * 3
        else:
            out += 1
    print(score)

Solution (Python):

"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0103&lang=jp
"""
import sys



class Baseball():
    def __init__(self):
        self.score = 0
        self.out_count = 0
        self.base = [0, 0, 0]

    def event(self, e):
        if e == 'HIT':
            self.base.insert(0, 1)
            home = self.base.pop()
            if home:
                self.score += 1
        elif e == 'HOMERUN':
            self.score += self.base.count(1)
            self.score += 1
            self.base = [0, 0, 0]
        elif e == 'OUT':
            self.out_count += 1


def main(args):
    num = int(input().strip())
    for _ in range(num):
        b = Baseball()
        while b.out_count < 3:
            b.event(input().strip())
        print(b.score)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):


import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


N = int(input())
for n in range(N):
    bases = [0, 0, 0]
    outs = 0
    score = 0
    while outs < 3:
        event = input().strip()
        if event == "OUT":
            outs += 1
        elif event == "HOMERUN":
            score += 1 + sum(bases)
            bases = [0, 0, 0]
        elif event == "HIT":
            score += bases[2]
            bases[1:] = bases[:2]
            bases[0] = 1
    print(score)

Solution (Python):
n = int(input())
for i in range(n):
	o_count = 0
	h_count = 0
	score = 0
	while True:
		site = input()
		if site == 'OUT':
			o_count += 1
			if o_count == 3:
				print(score)
				break
		elif site == 'HIT':
			if h_count == 3:
				score += 1
			else:
				h_count += 1
		else:
			score += h_count+1
			h_count = 0

Solution (Python):
import sys



def solve():
    n = int(sys.stdin.readline())*3
    s = 0
    p = 0
    while n>0:
        log = sys.stdin.readline()
        if "HIT" in log:
            if p >= 3:
                s += 1 
            else:
                p += 1
        elif "OUT" in log:
            n -= 1
            if n % 3 == 0:
                print(s)
                s = 0
                p = 0
                if n == 0:
                    break
        else:
            s += p + 1
            p = 0  
solve()

Solution (Python):
n = int(input())
for _ in range(n):
    n_out, n_run, score = 0, 0, 0
    while n_out < 3:
        evt = input()
        if evt == 'HIT':
            if n_run < 3:
                n_run += 1
            else:
                score += 1
        elif evt == 'HOMERUN':
            score += n_run + 1
            n_run = 0
        elif evt == 'OUT':
            n_out += 1
    print(score)

Solution (Python):
n = int(input())
inning = 0

while inning != n:
    score = 0
    info = [0, 0, 0]
    out_count = 0
    while True:
        event = input()
        if event == "OUT":
            out_count += 1
        if out_count == 3:
            inning += 1
            print(score)
            break

        if event == "HIT":
            score += info[-1]
            info = [1] + info[:2]
        elif event == "HOMERUN":
            score += sum(info) + 1
            info = [0, 0, 0]

Solution (Python):
gameCount = int(input())
output = []

for lp in range(gameCount):

    hitCount = 0
    outCount = 0
    score = 0

    while outCount < 3:

        content = input()

        if content == "HIT":
            if hitCount < 3:
                hitCount += 1
            else:
                score += 1
        elif content == "HOMERUN":
            score += hitCount
            score += 1
            hitCount = 0
        else:
            outCount += 1

    output.append(str(score))

print("\n".join(output))

Solution (Python):
for _ in[0]*int(input()):
 r=o=s=0
 while 1:
  a=input()[1]
  if'I'==a:
   if r<3:r+=1
   else:s+=1
  elif'O'==a:s+=r+1;r=0
  else:
   if o<2:o+=1
   else:print(s);break

Solution (Python):
for _ in[0]*int(input()):
 r=o=s=0
 while 1:
  a=input()[1]
  if'I'==a:
   if r<3:r+=1
   else:s+=1
  if'O'==a:s+=r+1;r=0
  if'U'==a:
   if o<2:o+=1
   else:print(s);break

Solution (Python):
n=int(input())
r=o=s=0
while n:
 a=input()[1]
 if'I'==a:
  if r<3:r+=1
  else:s+=1
 elif'O'==a:s+=r+1;r=0
 else:
  if o<2:o+=1
  else:print(s);n-=1;r=o=s=0

Solution (Python):
N = int(input())

for l in range(N):
    outCount = 0
    score = 0
    bases = 0
    while True:
        action = input()
        if action == "HIT":
            if bases == 3:
                score += 1
            else:
                bases += 1
        elif action == "HOMERUN":
            score += bases + 1
            bases = 0
        elif action == "OUT":
            outCount += 1

        if outCount == 3:
            print(score)
            break

Solution (Python):
event = []
inning = int(input())


NONE   = 0b000
FIRST  = 0b001
SECOND = 0b010
THIRD  = 0b100

runner = NONE
currentInning = 0
score = 0
outCount = 0
inningScore = []

while currentInning < inning:
    data = input()
    if data == "HIT":
        if (runner & THIRD) == THIRD:
            score += 1
            runner &= ~THIRD
        runner = runner << 1
        runner |= FIRST
    elif data == "OUT":
        outCount += 1

    elif data == "HOMERUN":
        if (runner & THIRD) == THIRD:
            score += 1
        if (runner & SECOND) == SECOND:
            score += 1
        if (runner & FIRST) == FIRST:
            score += 1
        score += 1
        runner = NONE
    
    if outCount == 3:
        inningScore.append(score)
        currentInning += 1
        outCount = 0
        score = 0
        runner = NONE

for i in range(inning):
    print(inningScore[i])

Solution (Python):
n = int(input())

for _ in range(n):
  out_cnt = 0
  runner = 0
  point = 0
  while out_cnt < 3:
    event = input()
    if event == "HIT":
      runner += 1
      if runner >= 4:
        point += 1
        runner = 3

    elif event == "HOMERUN":
      point += (runner + 1)
      runner = 0

    else:
      out_cnt += 1

  print(point)

Solution (Python):
inn=int(input())
o=0
i=0
s=[]
sc=0
ba=[]

while i<inn:
  b=input()
  if b == "OUT":
    o+=1
    if o%3 == 0 and o>0:
      s.append(sc)
      sc=0
      i+=1
      o=0
      ba=[]
  elif b == "HIT":
    ba.append(1)
    if len(ba)>=4:
      sc+=1
      ba=[1,1,1]      
  elif b == "HOMERUN":
    ba.append(1)
    sc+=len(ba)
    ba=[]
[print(i) for i in s]

Solution (Python):



for i in range(int(input())):
	p1 = p2 = p3 = out = cnt = 0;
	while True:
		s = input()
		if s == 'HIT':
			if p3 > 0: cnt += 1
			p3, p2, p1 = p2, p1, 1
		elif s == 'HOMERUN':
			cnt += p1 + p2 + p3 + 1
			p1 = p2 = p3 = 0
		else:
			out += 1
			if out == 3: break
	print(cnt)

Solution (Python):
import sys

base = [0, 0, 0]
out = 0

N = int(sys.stdin.readline())

point = 0
inning = list()

def choice(event):
    global base, out, point
    if event == "HIT":
        point += base.pop()
        base = [1] + base
    elif event == "HOMERUN":
        point += sum(base) + 1
        base = [0, 0, 0]
    elif event == "OUT":
        out += 1
        if out == 3:
            inning.append(point)
            point = 0
            out = 0
            base = [0, 0, 0]

for line in sys.stdin.readlines():
    line = line.strip()
    choice(line)

for i in inning:
    print i

Solution (Python):
line_num = int(raw_input())
out_all = line_num * 3
out_now = 0
out_inning = 0
score = 0
runner = 0
while out_all > out_now:
	if out_inning > 2:
		print score
		out_inning = 0
		score = 0
		runner = 0
	EVENT = raw_input()
	if EVENT.find("HOMERUN") > -1:
		score += 1 + runner
		runner = 0
	if EVENT.find("HIT") > -1:
		runner += 1
		if runner > 3:
			runner = 3
			score += 1
	if EVENT.find("OUT") > -1:
		out_inning += 1
		out_now += 1
print score

Solution (Python):
import sys
R = lambda:map(int,raw_input().split())
n = int(raw_input())

for i in xrange(n):
    run = []
    cnt = 0
    score = 0
    while cnt < 3:
        s = raw_input()
        if s == 'OUT': cnt += 1
        elif s == 'HIT':
            if len(run) is 3: score += 1
            else: run.append(1)
        else:
            score += (len(run)+1)
            run = []
    print score

Solution (Python):
for unused in xrange(input()):
    out,stack,p=0,0,0
    while out<3:
        ev=raw_input()
        if ev=="OUT":
            out+=1
        if ev=="HIT":
            stack+=1
        if ev=="HOMERUN":
            p+=stack+1
            stack=0
    if stack>=4:
        p+=stack-3
    print p

Solution (Python):
def hit(bases):
    got_point = False
    if bases[2]:
        got_point = True
    bases[0], bases[1], bases[2] = 1, bases[0], bases[1]
    if got_point:
        return 1
    else:
        return 0

def homerun(bases):
    num_runner = bases.count(1)
    bases[0], bases[1], bases[2] = 0, 0, 0
    return num_runner + 1

for i in range(int(raw_input())):
    bases = [0, 0, 0]
    out_count = 0
    points = 0
    while out_count != 3:
        event = raw_input()
        if event == "HIT":
            points += hit(bases)
        elif event == "HOMERUN":
            points += homerun(bases)
        else:
            out_count += 1
    print points