Artifact Content
Not logged in

Artifact 0bbb80b9b0468b3d487dd0c5bb968a476676d5a2


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

char memo[81*81*81*81];
int  n, X[16];
static const char NIL = 99;

bool win[243];

class TheStringGame
{
public:
	string winner(string s) 
	{
		// construct the winning condition table
		for(int mm=0; mm<243; ++mm)
			win[mm] = (mm/ 9%3==0 &&
					(  mm/81%3==2 && mm/27%3==1     // LOX
					|| mm/27%3==2 && mm/ 3%3==2     //  LXL
					|| mm/ 3%3==1 && mm/ 1%3==2 )); //   XOL

		// read the input
		int m=0, numX=0;
		{
			memset(memo, NIL, sizeof(memo));
			n = s.size();
			for(int i=0; i<n; ++i) {
				m *= 3;
				for(int j=0; j<numX; ++j)
					X[j] *= 3;
	
				if( s[i]=='X' )
					X[numX++] = 1;
				else
					m += (s[i]=='O' ? 1 : 2);
			}
		}

		// solve
		if( int r = negaMax(m, numX) )
		{
			stringstream sout;
			sout << (r>0 ? "John" : "Brus") << " " << numX-abs(r)+1;
			return sout.str();
		}
		return "Draw";
	}

	static int rev(int m)
	{
		int z = 0;
		for(int i=0; i<n; ++i)
			z = z*3 + m%3, m/=3;
		return z;
	}

	static char negaMax(int m, int numX)
	{
		if( numX == 0 )
			return 0;
		if( memo[m] != NIL )
			return memo[m];

		for(int j=0; j<numX; ++j)
			if( win[m*9/X[j]%243] )
				return memo[m] = numX;

		int sc = -numX;
		for(int j=0; j<numX; ++j) {
			int M = X[j];
			swap(X[j], X[numX-1]);
			sc = max(sc, -negaMax(m+M*1, numX-1));
			sc = max(sc, -negaMax(m+M*2, numX-1));
			swap(X[j], X[numX-1]);
		}
		return memo[m] = memo[rev(m)] = sc;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const string &Expected, const string &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string s = "XXOXXXLXLX"; 
	string RetVal = "John 1"; 
	return verify_case(RetVal, TheStringGame().winner(s)); }
int Test_(Case_<1>) {
	string s = "LXXLXXL"; 
	string RetVal = "Brus 2"; 
	return verify_case(RetVal, TheStringGame().winner(s)); }
int Test_(Case_<2>) {
	string s = "LLOOLLOOLLOOLLOO"; 
	string RetVal = "Draw"; 
	return verify_case(RetVal, TheStringGame().winner(s)); }
int Test_(Case_<3>) {
	string s = "XXXXXXXXXXXXXXXX"; 
	string RetVal = "Brus 16"; 
	return verify_case(RetVal, TheStringGame().winner(s)); }
int Test_(Case_<4>) {
	string s = "XXXXXXXXXXXXXXXO"; 
	string RetVal = "John 15"; 
	return verify_case(RetVal, TheStringGame().winner(s)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE