cc4202a4a6 2015-08-22 kinaba: #include <iostream> cc4202a4a6 2015-08-22 kinaba: #include <sstream> cc4202a4a6 2015-08-22 kinaba: #include <iomanip> cc4202a4a6 2015-08-22 kinaba: #include <vector> cc4202a4a6 2015-08-22 kinaba: #include <string> cc4202a4a6 2015-08-22 kinaba: #include <map> cc4202a4a6 2015-08-22 kinaba: #include <set> cc4202a4a6 2015-08-22 kinaba: #include <algorithm> cc4202a4a6 2015-08-22 kinaba: #include <numeric> cc4202a4a6 2015-08-22 kinaba: #include <iterator> cc4202a4a6 2015-08-22 kinaba: #include <functional> cc4202a4a6 2015-08-22 kinaba: #include <complex> cc4202a4a6 2015-08-22 kinaba: #include <queue> cc4202a4a6 2015-08-22 kinaba: #include <stack> cc4202a4a6 2015-08-22 kinaba: #include <cmath> cc4202a4a6 2015-08-22 kinaba: #include <cassert> cc4202a4a6 2015-08-22 kinaba: #include <tuple> cc4202a4a6 2015-08-22 kinaba: using namespace std; cc4202a4a6 2015-08-22 kinaba: typedef long long LL; cc4202a4a6 2015-08-22 kinaba: typedef complex<double> CMP; cc4202a4a6 2015-08-22 kinaba: cc4202a4a6 2015-08-22 kinaba: class ABBADiv1 { public: cc4202a4a6 2015-08-22 kinaba: string canObtain(string initial, string target) cc4202a4a6 2015-08-22 kinaba: { cc4202a4a6 2015-08-22 kinaba: return can(target, initial) ? "Possible" : "Impossible"; cc4202a4a6 2015-08-22 kinaba: } cc4202a4a6 2015-08-22 kinaba: cc4202a4a6 2015-08-22 kinaba: map<string, bool> memo; cc4202a4a6 2015-08-22 kinaba: bool can(const string& X, const string& S) cc4202a4a6 2015-08-22 kinaba: { cc4202a4a6 2015-08-22 kinaba: if(X == S) cc4202a4a6 2015-08-22 kinaba: return true; cc4202a4a6 2015-08-22 kinaba: if(X.size() < S.size()) cc4202a4a6 2015-08-22 kinaba: return false; cc4202a4a6 2015-08-22 kinaba: if(memo.count(X)) cc4202a4a6 2015-08-22 kinaba: return memo[X]; cc4202a4a6 2015-08-22 kinaba: if(X[X.size()-1] == 'A' && can(X.substr(0, X.size()-1), S)) return memo[X] = true; cc4202a4a6 2015-08-22 kinaba: if(X[0] == 'B' && can(rev(X.substr(1, X.size())), S)) return memo[X] = true; cc4202a4a6 2015-08-22 kinaba: return memo[X] = false; cc4202a4a6 2015-08-22 kinaba: } cc4202a4a6 2015-08-22 kinaba: cc4202a4a6 2015-08-22 kinaba: string rev(const string& x) { return string(x.rbegin(), x.rend()); } cc4202a4a6 2015-08-22 kinaba: }; cc4202a4a6 2015-08-22 kinaba: cc4202a4a6 2015-08-22 kinaba: // BEGIN CUT HERE cc4202a4a6 2015-08-22 kinaba: #include <ctime> cc4202a4a6 2015-08-22 kinaba: double start_time; string timer() cc4202a4a6 2015-08-22 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } cc4202a4a6 2015-08-22 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) cc4202a4a6 2015-08-22 kinaba: { os << "{ "; cc4202a4a6 2015-08-22 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) cc4202a4a6 2015-08-22 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } cc4202a4a6 2015-08-22 kinaba: void verify_case(const string& Expected, const string& Received) { cc4202a4a6 2015-08-22 kinaba: bool ok = (Expected == Received); cc4202a4a6 2015-08-22 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cc4202a4a6 2015-08-22 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } cc4202a4a6 2015-08-22 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); cc4202a4a6 2015-08-22 kinaba: #define END verify_case(_, ABBADiv1().canObtain(initial, target));} cc4202a4a6 2015-08-22 kinaba: int main(){ cc4202a4a6 2015-08-22 kinaba: cc4202a4a6 2015-08-22 kinaba: CASE(0) cc4202a4a6 2015-08-22 kinaba: string initial = "A"; cc4202a4a6 2015-08-22 kinaba: string target = "BABA"; cc4202a4a6 2015-08-22 kinaba: string _ = "Possible"; cc4202a4a6 2015-08-22 kinaba: END cc4202a4a6 2015-08-22 kinaba: CASE(1) cc4202a4a6 2015-08-22 kinaba: string initial = "BAAAAABAA"; cc4202a4a6 2015-08-22 kinaba: string target = "BAABAAAAAB"; cc4202a4a6 2015-08-22 kinaba: string _ = "Possible"; cc4202a4a6 2015-08-22 kinaba: END cc4202a4a6 2015-08-22 kinaba: CASE(2) cc4202a4a6 2015-08-22 kinaba: string initial = "A"; cc4202a4a6 2015-08-22 kinaba: string target = "ABBA"; cc4202a4a6 2015-08-22 kinaba: string _ = "Impossible"; cc4202a4a6 2015-08-22 kinaba: END cc4202a4a6 2015-08-22 kinaba: CASE(3) cc4202a4a6 2015-08-22 kinaba: string initial = "AAABBAABB"; cc4202a4a6 2015-08-22 kinaba: string target = "BAABAAABAABAABBBAAAAAABBAABBBBBBBABB"; cc4202a4a6 2015-08-22 kinaba: string _ = "Possible"; cc4202a4a6 2015-08-22 kinaba: END cc4202a4a6 2015-08-22 kinaba: CASE(4) cc4202a4a6 2015-08-22 kinaba: string initial = "AAABAAABB"; cc4202a4a6 2015-08-22 kinaba: string target = "BAABAAABAABAABBBAAAAAABBAABBBBBBBABB"; cc4202a4a6 2015-08-22 kinaba: string _ = "Impossible"; cc4202a4a6 2015-08-22 kinaba: END cc4202a4a6 2015-08-22 kinaba: /* cc4202a4a6 2015-08-22 kinaba: CASE(5) cc4202a4a6 2015-08-22 kinaba: string initial = ; cc4202a4a6 2015-08-22 kinaba: string target = ; cc4202a4a6 2015-08-22 kinaba: string _ = ; cc4202a4a6 2015-08-22 kinaba: END cc4202a4a6 2015-08-22 kinaba: CASE(6) cc4202a4a6 2015-08-22 kinaba: string initial = ; cc4202a4a6 2015-08-22 kinaba: string target = ; cc4202a4a6 2015-08-22 kinaba: string _ = ; cc4202a4a6 2015-08-22 kinaba: END cc4202a4a6 2015-08-22 kinaba: */ cc4202a4a6 2015-08-22 kinaba: } cc4202a4a6 2015-08-22 kinaba: // END CUT HERE