4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int gcd(int a, int b) { 4fd800b3a8 2011-02-23 kinaba: while(a) swap(a,b%=a); return b; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class Fractionalization { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector <string> partial(vector <string> definitions, int m, int n, string danceCall) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // input 4fd800b3a8 2011-02-23 kinaba: map< string, vector<string> > D; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<definitions.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: stringstream sin(definitions[i]); 4fd800b3a8 2011-02-23 kinaba: string name; sin>>name; name=name.substr(0, name.size()-1); 4fd800b3a8 2011-02-23 kinaba: vector<string> ds; 4fd800b3a8 2011-02-23 kinaba: for(string d; sin>>d;) 4fd800b3a8 2011-02-23 kinaba: ds.push_back(d); 4fd800b3a8 2011-02-23 kinaba: D[name] = ds; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // solve 4fd800b3a8 2011-02-23 kinaba: vector<string> result; 4fd800b3a8 2011-02-23 kinaba: while(m) { 4fd800b3a8 2011-02-23 kinaba: if( !D.count(danceCall) ) 4fd800b3a8 2011-02-23 kinaba: return vector<string>(1, "ILLEGAL"); 4fd800b3a8 2011-02-23 kinaba: vector<string>& d = D[danceCall]; 4fd800b3a8 2011-02-23 kinaba: int q = d.size(); 4fd800b3a8 2011-02-23 kinaba: int z = m*q/n; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<z; ++i) 4fd800b3a8 2011-02-23 kinaba: result.push_back(d[i]); 4fd800b3a8 2011-02-23 kinaba: m = m*q - z*n; 4fd800b3a8 2011-02-23 kinaba: danceCall = d[z]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const vector <string> &Expected, const vector <string> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string definitions_[] = { "FiveParts: One Two Three Four Five" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 4fd800b3a8 2011-02-23 kinaba: int m = 2; 4fd800b3a8 2011-02-23 kinaba: int n = 5; 4fd800b3a8 2011-02-23 kinaba: string danceCall = "FiveParts"; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"One", "Two" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string definitions_[] = { "Trade: Hinge Hinge", 4fd800b3a8 2011-02-23 kinaba: "TradeThreeTimes: Trade Trade Trade" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 4fd800b3a8 2011-02-23 kinaba: int m = 5; 4fd800b3a8 2011-02-23 kinaba: int n = 6; 4fd800b3a8 2011-02-23 kinaba: string danceCall = "TradeThreeTimes"; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"Trade", "Trade", "Hinge" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string definitions_[] = { "Trade: Hinge Hinge", 4fd800b3a8 2011-02-23 kinaba: "CenterTwoTrade: CenterTwoHinge CenterTwoHinge", 4fd800b3a8 2011-02-23 kinaba: "SpinTheTop: Trade FanTheTop", 4fd800b3a8 2011-02-23 kinaba: "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 4fd800b3a8 2011-02-23 kinaba: int m = 1; 4fd800b3a8 2011-02-23 kinaba: int n = 2; 4fd800b3a8 2011-02-23 kinaba: string danceCall = "HotFootSpin"; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"FanTheTop", "CenterTwoHinge" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string definitions_[] = { "Trade: Hinge Hinge", 4fd800b3a8 2011-02-23 kinaba: "CenterTwoTrade: CenterTwoHinge CenterTwoHinge", 4fd800b3a8 2011-02-23 kinaba: "SpinTheTop: Trade FanTheTop", 4fd800b3a8 2011-02-23 kinaba: "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 4fd800b3a8 2011-02-23 kinaba: int m = 3; 4fd800b3a8 2011-02-23 kinaba: int n = 4; 4fd800b3a8 2011-02-23 kinaba: string danceCall = "HotFootSpin"; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"FanTheTop", "CenterTwoTrade", "Hinge" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string definitions_[] = { "A: One Two", 4fd800b3a8 2011-02-23 kinaba: "B: One Two Three", 4fd800b3a8 2011-02-23 kinaba: "C: One Two Three Four" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 4fd800b3a8 2011-02-23 kinaba: int m = 3; 4fd800b3a8 2011-02-23 kinaba: int n = 4; 4fd800b3a8 2011-02-23 kinaba: string danceCall = "C"; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"One", "Two", "Three" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string definitions_[] = { "A: X Y Z", 4fd800b3a8 2011-02-23 kinaba: "B: A A A" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 4fd800b3a8 2011-02-23 kinaba: int m = 7; 4fd800b3a8 2011-02-23 kinaba: int n = 23; 4fd800b3a8 2011-02-23 kinaba: string danceCall = "B"; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"ILLEGAL" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: string definitions_[] = { "A: B C", 4fd800b3a8 2011-02-23 kinaba: "D: E F" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 4fd800b3a8 2011-02-23 kinaba: int m = 1; 4fd800b3a8 2011-02-23 kinaba: int n = 2; 4fd800b3a8 2011-02-23 kinaba: string danceCall = "G"; 4fd800b3a8 2011-02-23 kinaba: string RetVal_[] = {"ILLEGAL" }; 4fd800b3a8 2011-02-23 kinaba: vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: