6d771c8da1 2011-07-10 kinaba: #include <iostream> 6d771c8da1 2011-07-10 kinaba: #include <sstream> 6d771c8da1 2011-07-10 kinaba: #include <iomanip> 6d771c8da1 2011-07-10 kinaba: #include <vector> 6d771c8da1 2011-07-10 kinaba: #include <string> 6d771c8da1 2011-07-10 kinaba: #include <map> 6d771c8da1 2011-07-10 kinaba: #include <set> 6d771c8da1 2011-07-10 kinaba: #include <algorithm> 6d771c8da1 2011-07-10 kinaba: #include <numeric> 6d771c8da1 2011-07-10 kinaba: #include <iterator> 6d771c8da1 2011-07-10 kinaba: #include <functional> 6d771c8da1 2011-07-10 kinaba: #include <complex> 6d771c8da1 2011-07-10 kinaba: #include <queue> 6d771c8da1 2011-07-10 kinaba: #include <stack> 6d771c8da1 2011-07-10 kinaba: #include <cmath> 6d771c8da1 2011-07-10 kinaba: #include <cassert> 6d771c8da1 2011-07-10 kinaba: #include <cstring> 6d771c8da1 2011-07-10 kinaba: using namespace std; 6d771c8da1 2011-07-10 kinaba: typedef long long LL; 6d771c8da1 2011-07-10 kinaba: typedef complex<double> CMP; 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: int gcd(int x, int y) 6d771c8da1 2011-07-10 kinaba: { 6d771c8da1 2011-07-10 kinaba: while(x) 6d771c8da1 2011-07-10 kinaba: swap(x, y%=x); 6d771c8da1 2011-07-10 kinaba: return y; 6d771c8da1 2011-07-10 kinaba: } 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: int lcm(int x, int y) 6d771c8da1 2011-07-10 kinaba: { 6d771c8da1 2011-07-10 kinaba: return x/gcd(x,y)*y; 6d771c8da1 2011-07-10 kinaba: } 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: class ArtShift { public: 6d771c8da1 2011-07-10 kinaba: int maxShifts(string sequence) 6d771c8da1 2011-07-10 kinaba: { 6d771c8da1 2011-07-10 kinaba: int B=0, W=0; 6d771c8da1 2011-07-10 kinaba: for(int i=0; i<sequence.size(); ++i) 6d771c8da1 2011-07-10 kinaba: (sequence[i]=='B' ? B : W)++; 6d771c8da1 2011-07-10 kinaba: memo.assign(31*31, set<int>()); 6d771c8da1 2011-07-10 kinaba: for(int b=0; b<=B; ++b) memo[b*31+0].insert(1); 6d771c8da1 2011-07-10 kinaba: for(int w=0; w<=W; ++w) memo[0*31+w].insert(1); 6d771c8da1 2011-07-10 kinaba: return *rec(B,W).rbegin(); 6d771c8da1 2011-07-10 kinaba: } 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: vector< set<int> > memo; 6d771c8da1 2011-07-10 kinaba: const set<int>& rec(int B, int W) 6d771c8da1 2011-07-10 kinaba: { 6d771c8da1 2011-07-10 kinaba: set<int>& result = memo[B*31+W]; 6d771c8da1 2011-07-10 kinaba: if( !result.empty() ) 6d771c8da1 2011-07-10 kinaba: return result; 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: result.insert(B+W); 6d771c8da1 2011-07-10 kinaba: for(int b= 0; b<=B; ++b) 6d771c8da1 2011-07-10 kinaba: for(int w=!b; w<=W; ++w) { 6d771c8da1 2011-07-10 kinaba: const set<int>& x = rec(b,w); 6d771c8da1 2011-07-10 kinaba: const set<int>& y = rec(B-b,W-w); 6d771c8da1 2011-07-10 kinaba: for(set<int>::const_iterator it=x.begin(); it!=x.end(); ++it) 6d771c8da1 2011-07-10 kinaba: for(set<int>::const_iterator jt=y.begin(); jt!=y.end(); ++jt) 6d771c8da1 2011-07-10 kinaba: result.insert(lcm(*it,*jt)); 6d771c8da1 2011-07-10 kinaba: } 6d771c8da1 2011-07-10 kinaba: return result; 6d771c8da1 2011-07-10 kinaba: } 6d771c8da1 2011-07-10 kinaba: }; 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: // BEGIN CUT HERE 6d771c8da1 2011-07-10 kinaba: #include <ctime> 6d771c8da1 2011-07-10 kinaba: double start_time; string timer() 6d771c8da1 2011-07-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6d771c8da1 2011-07-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6d771c8da1 2011-07-10 kinaba: { os << "{ "; 6d771c8da1 2011-07-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6d771c8da1 2011-07-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6d771c8da1 2011-07-10 kinaba: void verify_case(const int& Expected, const int& Received) { 6d771c8da1 2011-07-10 kinaba: bool ok = (Expected == Received); 6d771c8da1 2011-07-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6d771c8da1 2011-07-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6d771c8da1 2011-07-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6d771c8da1 2011-07-10 kinaba: #define END verify_case(_, ArtShift().maxShifts(sequence));} 6d771c8da1 2011-07-10 kinaba: int main(){ 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: CASE(0) 6d771c8da1 2011-07-10 kinaba: string sequence = "BW"; 6d771c8da1 2011-07-10 kinaba: int _ = 2; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(1) 6d771c8da1 2011-07-10 kinaba: string sequence = "BBBWBBB"; 6d771c8da1 2011-07-10 kinaba: int _ = 7; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(2) 6d771c8da1 2011-07-10 kinaba: string sequence = "BWBWB"; 6d771c8da1 2011-07-10 kinaba: int _ = 6; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(3) 6d771c8da1 2011-07-10 kinaba: string sequence = "WWWWWWWWW"; 6d771c8da1 2011-07-10 kinaba: int _ = 1; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(4) 6d771c8da1 2011-07-10 kinaba: string sequence = "WWWWWWWWWBBWB"; 6d771c8da1 2011-07-10 kinaba: int _ = 60; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(5) 6d771c8da1 2011-07-10 kinaba: string sequence = "W"; 6d771c8da1 2011-07-10 kinaba: int _ = 1; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(6) 6d771c8da1 2011-07-10 kinaba: string sequence = "B"; 6d771c8da1 2011-07-10 kinaba: int _ = 1; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(7) 6d771c8da1 2011-07-10 kinaba: string sequence = "BB"; 6d771c8da1 2011-07-10 kinaba: int _ = 1; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: CASE(8) 6d771c8da1 2011-07-10 kinaba: string sequence = "BWBWBWBWBWBWBWBWBWBWBWBWBWBWBW"; 6d771c8da1 2011-07-10 kinaba: int _ = -1; 6d771c8da1 2011-07-10 kinaba: END 6d771c8da1 2011-07-10 kinaba: 6d771c8da1 2011-07-10 kinaba: } 6d771c8da1 2011-07-10 kinaba: // END CUT HERE