Artifact Content
Not logged in

Artifact e8ec418b9332bc9f23e92ad7e3cd5e9b09370fdc


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class RowAndCoins { public:
	string getWinner(string cells)
	{
		int n = cells.size();
		int v = 0;
		for(int i=0; i<n; ++i)
			v = (v<<1) | (cells[i]=='B');
		memo.clear();
		return rec(0, v, n, 0) ? "Bob" : "Alice";
	}

	vector<int> memo;
	int rec(int p, const int v, const int n, int mask)
	{
		int key = mask*2+p;
		if( key >= memo.size() )
			memo.resize(key+1, -1);
		if( memo[key] >= 0 )
			return memo[key];

		for(int i=0; i<n; ++i)
			if( mask == (1<<n)-1 - (1<<i) )
				return memo[key] = (v&(1<<i) ? 1 : 0);

		bool p_win = false;
		for(int s=0; s<n; ++s)
			for(int e=s+1; e<=n; ++e) if( e-s != n ) {
				int mm = mask;
				for(int i=s; i<e; ++i) {
					if( mask&(1<<i) )
						goto next;
					mm |= (1<<i);
				}
				if( mm == (1<<n)-1 )
					goto next;
				if( p == rec(p^1, v, n, mm) )
					p_win = true;
			next:;
			}
		return memo[key] = (p_win ?  p : p^1);
	}
};

// 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 string& Expected, const string& 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(_, RowAndCoins().getWinner(cells));}
int main(){

CASE(0)
	string cells = "ABBB"; 
	string _ = "Alice"; 
END
CASE(1)
	string cells = "BBBB"; 
	string _ = "Bob"; 
END
CASE(2)
	string cells = "BA"; 
	string _ = "Alice"; 
END
CASE(3)
	string cells = "BABBABBB"; 
	string _ = "Bob"; 
END
CASE(4)
	string cells = "ABABBBABAABBAA"; 
	string _ = "Alice"; 
END
CASE(5)
	string cells = "BBBAAABBAAABBB"; 
	string _ = "Bob"; 
END
CASE(6)
	string cells = "A"; 
	string _ = "Alice"; 
END
CASE(7)
	string cells = "B"; 
	string _ = "Bob"; 
END

}
// END CUT HERE