Artifact Content
Not logged in

Artifact f8183ab5eda22bf1839698f2b86e9e725e443c06


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

enum {HH, HS, SH, SS};

class TheKingsArmyDiv1 { public:
	int getNumber(vector <string> state)
	{
		const int N = state[0].size();

		vector<int> s;
		for(int i=0; i<N; ++i) 
			s.push_back((state[0][i]=='S')*2 + (state[1][i]=='S'));

		const int nSS = count(s.begin(), s.end(), SS);
		if(nSS == N)
			return -1;

		vector<vector<int>> blocks;
		for(int i=0; i<N; ) {
			while(i<N && s[i]==SS) ++i;
			if(i<N) {
				blocks.emplace_back();
				while(i<N && s[i]!=SS) {
					if(s[i] != HH)
						blocks.back().push_back(s[i]);
					++i;
				}
				if(blocks.back().empty())
					blocks.pop_back();
			}
		}

		int c1=0, c2=0;
		for(auto& bl: blocks) {
			c1 += align_cost(bl, HS);
			c2 += align_cost(bl, SH);
		}
		return nSS + (blocks.empty() ? 0 : min(c1, c2)+1);
	}

	int align_cost(const vector<int>& b, int type)
	{
		int cost = 0;
		for(int i=0; i<b.size(); ) {
			while(i<b.size() && b[i]==type) ++i;
			if(i<b.size()) {
				while(i<b.size() && b[i]!=type) ++i;
				++cost;
			}
		}
		return cost;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, TheKingsArmyDiv1().getNumber(state));}
int main(){

CASE(0)
	string state_[] = {"HSH", 
 "SHS"};
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = 2; 
END
CASE(1)
	string state_[] = {"HH", 
 "HH"};
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = 0; 
END
CASE(2)
	string state_[] = {"HHHHH", 
 "HSHSH"};
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = 1; 
END
CASE(3)
	string state_[] = {"S", 
 "S"};
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = -1; 
END
CASE(4)
	string state_[] = {"HSHHSHSHSHHHSHSHSH", 
 "SSSSHSSHSHSHHSSSSH"};
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = 8; 
END
CASE(5)
	string state_[] = {"HS",
 "HS"};
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = 1; 
END
CASE(6)
string state_[] = {"HHH", "SSS"};
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = 1; 
END
/*
CASE(7)
	string state_[] = ;
	  vector <string> state(state_, state_+sizeof(state_)/sizeof(*state_)); 
	int _ = ; 
END
*/
}
// END CUT HERE