98499b8c6b 2011-11-12 kinaba: #include <iostream> 98499b8c6b 2011-11-12 kinaba: #include <sstream> 98499b8c6b 2011-11-12 kinaba: #include <iomanip> 98499b8c6b 2011-11-12 kinaba: #include <vector> 98499b8c6b 2011-11-12 kinaba: #include <string> 98499b8c6b 2011-11-12 kinaba: #include <map> 98499b8c6b 2011-11-12 kinaba: #include <set> 98499b8c6b 2011-11-12 kinaba: #include <algorithm> 98499b8c6b 2011-11-12 kinaba: #include <numeric> 98499b8c6b 2011-11-12 kinaba: #include <iterator> 98499b8c6b 2011-11-12 kinaba: #include <functional> 98499b8c6b 2011-11-12 kinaba: #include <complex> 98499b8c6b 2011-11-12 kinaba: #include <queue> 98499b8c6b 2011-11-12 kinaba: #include <stack> 98499b8c6b 2011-11-12 kinaba: #include <cmath> 98499b8c6b 2011-11-12 kinaba: #include <cassert> 98499b8c6b 2011-11-12 kinaba: #include <cstring> 98499b8c6b 2011-11-12 kinaba: using namespace std; 98499b8c6b 2011-11-12 kinaba: typedef long long LL; 98499b8c6b 2011-11-12 kinaba: typedef complex<double> CMP; 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: class RowAndCoins { public: 98499b8c6b 2011-11-12 kinaba: string getWinner(string cells) 98499b8c6b 2011-11-12 kinaba: { 98499b8c6b 2011-11-12 kinaba: int n = cells.size(); 98499b8c6b 2011-11-12 kinaba: int v = 0; 98499b8c6b 2011-11-12 kinaba: for(int i=0; i<n; ++i) 98499b8c6b 2011-11-12 kinaba: v = (v<<1) | (cells[i]=='B'); 98499b8c6b 2011-11-12 kinaba: memo.clear(); 98499b8c6b 2011-11-12 kinaba: return rec(0, v, n, 0) ? "Bob" : "Alice"; 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: vector<int> memo; 98499b8c6b 2011-11-12 kinaba: int rec(int p, const int v, const int n, int mask) 98499b8c6b 2011-11-12 kinaba: { 98499b8c6b 2011-11-12 kinaba: int key = mask*2+p; 98499b8c6b 2011-11-12 kinaba: if( key >= memo.size() ) 98499b8c6b 2011-11-12 kinaba: memo.resize(key+1, -1); 98499b8c6b 2011-11-12 kinaba: if( memo[key] >= 0 ) 98499b8c6b 2011-11-12 kinaba: return memo[key]; 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: for(int i=0; i<n; ++i) 98499b8c6b 2011-11-12 kinaba: if( mask == (1<<n)-1 - (1<<i) ) 98499b8c6b 2011-11-12 kinaba: return memo[key] = (v&(1<<i) ? 1 : 0); 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: bool p_win = false; 98499b8c6b 2011-11-12 kinaba: for(int s=0; s<n; ++s) 98499b8c6b 2011-11-12 kinaba: for(int e=s+1; e<=n; ++e) if( e-s != n ) { 98499b8c6b 2011-11-12 kinaba: int mm = mask; 98499b8c6b 2011-11-12 kinaba: for(int i=s; i<e; ++i) { 98499b8c6b 2011-11-12 kinaba: if( mask&(1<<i) ) 98499b8c6b 2011-11-12 kinaba: goto next; 98499b8c6b 2011-11-12 kinaba: mm |= (1<<i); 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: if( mm == (1<<n)-1 ) 98499b8c6b 2011-11-12 kinaba: goto next; 98499b8c6b 2011-11-12 kinaba: if( p == rec(p^1, v, n, mm) ) 98499b8c6b 2011-11-12 kinaba: p_win = true; 98499b8c6b 2011-11-12 kinaba: next:; 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: return memo[key] = (p_win ? p : p^1); 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: }; 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: // BEGIN CUT HERE 98499b8c6b 2011-11-12 kinaba: #include <ctime> 98499b8c6b 2011-11-12 kinaba: double start_time; string timer() 98499b8c6b 2011-11-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 98499b8c6b 2011-11-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 98499b8c6b 2011-11-12 kinaba: { os << "{ "; 98499b8c6b 2011-11-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 98499b8c6b 2011-11-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 98499b8c6b 2011-11-12 kinaba: void verify_case(const string& Expected, const string& Received) { 98499b8c6b 2011-11-12 kinaba: bool ok = (Expected == Received); 98499b8c6b 2011-11-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 98499b8c6b 2011-11-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 98499b8c6b 2011-11-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 98499b8c6b 2011-11-12 kinaba: #define END verify_case(_, RowAndCoins().getWinner(cells));} 98499b8c6b 2011-11-12 kinaba: int main(){ 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: CASE(0) 98499b8c6b 2011-11-12 kinaba: string cells = "ABBB"; 98499b8c6b 2011-11-12 kinaba: string _ = "Alice"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(1) 98499b8c6b 2011-11-12 kinaba: string cells = "BBBB"; 98499b8c6b 2011-11-12 kinaba: string _ = "Bob"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(2) 98499b8c6b 2011-11-12 kinaba: string cells = "BA"; 98499b8c6b 2011-11-12 kinaba: string _ = "Alice"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(3) 98499b8c6b 2011-11-12 kinaba: string cells = "BABBABBB"; 98499b8c6b 2011-11-12 kinaba: string _ = "Bob"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(4) 98499b8c6b 2011-11-12 kinaba: string cells = "ABABBBABAABBAA"; 98499b8c6b 2011-11-12 kinaba: string _ = "Alice"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(5) 98499b8c6b 2011-11-12 kinaba: string cells = "BBBAAABBAAABBB"; 98499b8c6b 2011-11-12 kinaba: string _ = "Bob"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(6) 98499b8c6b 2011-11-12 kinaba: string cells = "A"; 98499b8c6b 2011-11-12 kinaba: string _ = "Alice"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(7) 98499b8c6b 2011-11-12 kinaba: string cells = "B"; 98499b8c6b 2011-11-12 kinaba: string _ = "Bob"; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: // END CUT HERE