17966b23b6 2015-02-10 kinaba: #include <iostream> 17966b23b6 2015-02-10 kinaba: #include <sstream> 17966b23b6 2015-02-10 kinaba: #include <iomanip> 17966b23b6 2015-02-10 kinaba: #include <vector> 17966b23b6 2015-02-10 kinaba: #include <string> 17966b23b6 2015-02-10 kinaba: #include <map> 17966b23b6 2015-02-10 kinaba: #include <set> 17966b23b6 2015-02-10 kinaba: #include <algorithm> 17966b23b6 2015-02-10 kinaba: #include <numeric> 17966b23b6 2015-02-10 kinaba: #include <iterator> 17966b23b6 2015-02-10 kinaba: #include <functional> 17966b23b6 2015-02-10 kinaba: #include <complex> 17966b23b6 2015-02-10 kinaba: #include <queue> 17966b23b6 2015-02-10 kinaba: #include <stack> 17966b23b6 2015-02-10 kinaba: #include <cmath> 17966b23b6 2015-02-10 kinaba: #include <cassert> 17966b23b6 2015-02-10 kinaba: #include <tuple> 17966b23b6 2015-02-10 kinaba: using namespace std; 17966b23b6 2015-02-10 kinaba: typedef long long LL; 17966b23b6 2015-02-10 kinaba: typedef complex<double> CMP; 17966b23b6 2015-02-10 kinaba: 17966b23b6 2015-02-10 kinaba: static const char* BAD = "XXX"; 17966b23b6 2015-02-10 kinaba: 17966b23b6 2015-02-10 kinaba: class AB { public: 17966b23b6 2015-02-10 kinaba: map<tuple<int,int,int>, string> memo; 17966b23b6 2015-02-10 kinaba: string create(int N, int KPair, int NumB) 17966b23b6 2015-02-10 kinaba: { 17966b23b6 2015-02-10 kinaba: if(N==0) 17966b23b6 2015-02-10 kinaba: return (KPair==0 && NumB==0 ? "" : BAD); 17966b23b6 2015-02-10 kinaba: 17966b23b6 2015-02-10 kinaba: tuple<int,int,int> key(N, KPair, NumB); 17966b23b6 2015-02-10 kinaba: if(memo.count(key)) 17966b23b6 2015-02-10 kinaba: return memo[key]; 17966b23b6 2015-02-10 kinaba: 17966b23b6 2015-02-10 kinaba: auto a = create(N-1, KPair-NumB, NumB); 17966b23b6 2015-02-10 kinaba: if(a != BAD) 17966b23b6 2015-02-10 kinaba: return memo[key] = "A" + a; 17966b23b6 2015-02-10 kinaba: if(NumB>=1) { 17966b23b6 2015-02-10 kinaba: auto b = create(N-1, KPair, NumB-1); 17966b23b6 2015-02-10 kinaba: if(b != BAD) 17966b23b6 2015-02-10 kinaba: return memo[key] = "B" + b; 17966b23b6 2015-02-10 kinaba: } 17966b23b6 2015-02-10 kinaba: return memo[key] = BAD; 17966b23b6 2015-02-10 kinaba: } 17966b23b6 2015-02-10 kinaba: 17966b23b6 2015-02-10 kinaba: string createString(int N, int K) 17966b23b6 2015-02-10 kinaba: { 17966b23b6 2015-02-10 kinaba: for(int NumB=0; NumB<=N; ++NumB) { 17966b23b6 2015-02-10 kinaba: auto s = create(N, K, NumB); 17966b23b6 2015-02-10 kinaba: if(s != BAD) 17966b23b6 2015-02-10 kinaba: return s; 17966b23b6 2015-02-10 kinaba: } 17966b23b6 2015-02-10 kinaba: return ""; 17966b23b6 2015-02-10 kinaba: } 17966b23b6 2015-02-10 kinaba: }; 17966b23b6 2015-02-10 kinaba: 17966b23b6 2015-02-10 kinaba: // BEGIN CUT HERE 17966b23b6 2015-02-10 kinaba: #include <ctime> 17966b23b6 2015-02-10 kinaba: double start_time; string timer() 17966b23b6 2015-02-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 17966b23b6 2015-02-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 17966b23b6 2015-02-10 kinaba: { os << "{ "; 17966b23b6 2015-02-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 17966b23b6 2015-02-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 17966b23b6 2015-02-10 kinaba: void verify_case(const string& Expected, const string& Received) { 17966b23b6 2015-02-10 kinaba: bool ok = (Expected == Received); 17966b23b6 2015-02-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 17966b23b6 2015-02-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 17966b23b6 2015-02-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 17966b23b6 2015-02-10 kinaba: #define END verify_case(_, AB().createString(N, K));} 17966b23b6 2015-02-10 kinaba: int main(){ 17966b23b6 2015-02-10 kinaba: 17966b23b6 2015-02-10 kinaba: CASE(0) 17966b23b6 2015-02-10 kinaba: int N = 3; 17966b23b6 2015-02-10 kinaba: int K = 2; 17966b23b6 2015-02-10 kinaba: string _ = "ABB"; 17966b23b6 2015-02-10 kinaba: END 17966b23b6 2015-02-10 kinaba: CASE(1) 17966b23b6 2015-02-10 kinaba: int N = 2; 17966b23b6 2015-02-10 kinaba: int K = 0; 17966b23b6 2015-02-10 kinaba: string _ = "BA"; 17966b23b6 2015-02-10 kinaba: END 17966b23b6 2015-02-10 kinaba: CASE(2) 17966b23b6 2015-02-10 kinaba: int N = 5; 17966b23b6 2015-02-10 kinaba: int K = 8; 17966b23b6 2015-02-10 kinaba: string _ = ""; 17966b23b6 2015-02-10 kinaba: END 17966b23b6 2015-02-10 kinaba: CASE(3) 17966b23b6 2015-02-10 kinaba: int N = 10; 17966b23b6 2015-02-10 kinaba: int K = 12; 17966b23b6 2015-02-10 kinaba: string _ = "BAABBABAAB"; 17966b23b6 2015-02-10 kinaba: END 17966b23b6 2015-02-10 kinaba: /* 17966b23b6 2015-02-10 kinaba: CASE(4) 17966b23b6 2015-02-10 kinaba: int N = ; 17966b23b6 2015-02-10 kinaba: int K = ; 17966b23b6 2015-02-10 kinaba: string _ = ; 17966b23b6 2015-02-10 kinaba: END 17966b23b6 2015-02-10 kinaba: CASE(5) 17966b23b6 2015-02-10 kinaba: int N = ; 17966b23b6 2015-02-10 kinaba: int K = ; 17966b23b6 2015-02-10 kinaba: string _ = ; 17966b23b6 2015-02-10 kinaba: END 17966b23b6 2015-02-10 kinaba: */ 17966b23b6 2015-02-10 kinaba: } 17966b23b6 2015-02-10 kinaba: // END CUT HERE