Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	n, _ := strconv.Atoi(stdin.Text())
	for i := 0; i < n; i++ {
		base := [3]bool{}
		score, out := 0, 0
		for stdin.Scan() {
			switch stdin.Text() {
			case "HIT":
				if base[2] { // 3塁
					score++
				}
				if base[1] { // 2塁
					base[2] = true
					base[1] = false
				}
				if base[0] { // 1塁
					base[1] = true
				}
				base[0] = true
			case "HOMERUN":
				for i := 0; i < 3; i++ {
					if base[i] {
						score++
						base[i] = false
					}
				}
				score++
			case "OUT":
				out++
			}
			if out >= 3 {
				fmt.Println(score)
				break
			}
		}
	}
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cmath>
#define pb(in,tmp) in.push_back(tmp)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) loop(i,0,b)
using namespace std;
int main(){
int n;
cin>>n;
rep(i,n){
int out=0;
int r1=0,r2=0,r3=0;
int tm=0;
while(out!=3){
char in[10];
cin>>in;
if(strcmp(in,"OUT")==0)out++;
if(strcmp(in,"HIT")==0){
if(r3==1){tm++;r3=0;}
if(r2==1){r3=1;r2=0;}
if(r1==1){r2=1;r1=0;}
r1++;
}
if(strcmp(in,"HOMERUN")==0){
if(r1==1){tm++;r1=0;}
if(r2==1){tm++;r2=0;}
if(r3==1){tm++;r3=0;}
tm++;
}
}
cout<<tm<<endl;
}
}

C++: #include <iostream>
#include <string>
#include <bitset>
  
class Baseball{
    u_int score, out_count;
    std::bitset<4> runners;
public:
    Baseball() : score(), out_count(){} //initializer list
    ~Baseball() = default;
    void hit();
    void homerun();
    void out() {++out_count;}
    u_int get_score() {return score;}
    u_int get_out() {return out_count;}
};
 
void Baseball::hit(){
    runners <<= 1; //shift
	runners.set(0); //set runner
    if(runners.test(3)){
        ++score;
        runners.reset(3);
    }
}
 
void Baseball::homerun(){
    score += runners.count() + 1;
    runners.reset();
}
 
int main(){
    u_int inning;
    std::cin >> inning;
    
    for(u_int i = 0; i < inning; ++i){
        Baseball game;
        while(game.get_out() != 3){
            std::string event;
            std::cin >> event;
            
            // check event
            if(event == "HIT"){
                game.hit();
            }else if(event == "HOMERUN"){
                game.homerun();
            }else if(event == "OUT"){
                game.out();
            }
        }
        std::cout << game.get_score() << std::endl; //show score
    }
    
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
	int n;
	cin >> n;

	while(n--)
	{
		int out = 0;
		vint v(4, 0);

		while(out != 3)
		{
			string ev;
			cin >> ev;

			if(ev == "OUT") out++;
			else if(ev == "HIT")
			{
				rep(i, 3)
				{
					v[3 - i] += v[2 - i];
					v[2 - i] = i == 2;
				}
			}
			else
			{
				v[3] += accumulate(v.begin(), v.begin() + 3, 1);
				fill(v.begin(), v.begin() + 3, 0);
			}
		}

		cout << v[3] << endl;
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main(){

  int n;
  string daseki;
  cin >> n;

  for(int i=0;i<n;i++){

    int a[5] = {0},count = 0,out = 0;

    while(1){
      if(out == 3) break;

      cin >> daseki;

      if(daseki == "HIT"){
	if(a[3] == 1){
	  count++;
	  a[3] = 0;
	}
	if(a[2] == 1){
	  a[3] = 1;
	  a[2] = 0;
	}
	if(a[1] == 1){
	  a[2] = 1;
	  a[1] = 0;
	}
	a[1] = 1;
      }

      if(daseki == "HOMERUN"){
	count++;
	for(int j=1;j<=3;j++){
	  if(a[j] == 1){
	    count++;
	    a[j] = 0;
	  }
	}
      }

      if(daseki == "OUT") out++;
    }

    cout << count << endl;

  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define MOD 1000000007
#define ALL(a) begin((a)),end((a))
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
typedef vector<edge> edges;
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///


int main()
{
	int n;
	cin >> n;
	REP(x, n)
	{
		string s;
		int cntout = 0;
		int run = 0;
		int score = 0;
		while (cin >> s)
		{
			if (s == "OUT") cntout++;
			if (cntout == 3)
			{
				cout << score << endl;
				break;
			}
			if (s == "HIT")
			{
				run <<= 1;
				run += 1;
				if (run >= 8)
				{
					score++;
					run %= 8;
				}
			}
			if (s == "HOMERUN")
			{
				score++;
				while (run > 0)
				{
					if (run & 1) score++;
					run >>= 1;
				}
			}
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <string>
using namespace std;

int n;
char buf[1000000];

int main() {
  scanf("%d ", &n);
  while (n--) {
    int cnt = 0;
    int score = 0;
    bool runner[4] = {false};

    while (cnt < 3) {
      runner[0] = true;

      scanf("%s", buf);
  
      string s(buf);
      if (s == "HIT") {
        if (runner[3]) score++;
        for (int i=2; i>=0; i--) {
          runner[i+1] = runner[i]; 
        }
      } else if (s == "HOMERUN") {
        for (int i=0; i<4; i++) {
          if (runner[i]) score++;
          runner[i] = false;
        }
      } else if (s == "OUT") {
        cnt++;
      }
    }

    printf("%d\n", score);
  }
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime> 

using namespace std;

int main(void){
	int n;
	int a,b;
	int sc=0,oc=0,lun[4]={0,0,0,0,};
	cin >> n;
	for(a=0;a<n;a++){
		sc=0;
		oc=0;
		for(b=0;b<4;b++)lun[b]=0;
		while(oc<3){
			string str;
			getline(cin,str);
			if(str=="HIT"){
				if(lun[3]==1)sc++,lun[3]=0;
				lun[3]=lun[2];
				lun[2]=lun[1];
				lun[1]=1;
			}
			if(str=="HOMERUN"){
				for(b=1;b<4;b++)sc+=lun[b],lun[b]=0;
				sc++;
			}
			if(str=="OUT")oc++;
		}
		cout << sc << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>

int main(void)
{
	int base=0,out=0,score=0,e,i,j;
	char str[10];
	scanf("%d",&e);
	if(e==0)break;
	for(i=0;i<e;){
		scanf("%s",&str);
		if(!strcmp(str,"HIT"))base++;
		
		else if(!strcmp(str,"OUT")){
			out++;
		}
		
		else if(!strcmp(str,"HOMERUN")){
			score=score+base+1;
			base=0;
		}
		if(base>3){
			base--;
			score++;
		}
		if(out==3){
			out=0;
			printf("%d\n",score);
			i++;
			base=0;
			score=0;
		}
	}
	return 0;
}