Artifact Content
Not logged in

Artifact 0008d0ab74cb7d3e74cc7234b3da74fd80b6f23a


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

int gcd(int a, int b) {
	while(a) swap(a,b%=a); return b;
}

class Fractionalization {
public:
	vector <string> partial(vector <string> definitions, int m, int n, string danceCall) 
	{
		// input
		map< string, vector<string> > D;
		for(int i=0; i<definitions.size(); ++i) {
			stringstream sin(definitions[i]);
			string name; sin>>name; name=name.substr(0, name.size()-1);
			vector<string> ds;
			for(string d; sin>>d;)
				ds.push_back(d);
			D[name] = ds;
		}

		// solve
		vector<string> result;
		while(m) {
			if( !D.count(danceCall) )
				return vector<string>(1, "ILLEGAL");
			vector<string>& d = D[danceCall];
			int q = d.size();
			int z = m*q/n;
			for(int i=0; i<z; ++i)
				result.push_back(d[i]);
			m = m*q - z*n;
			danceCall = d[z];
		}
		return result;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string definitions_[] = { "FiveParts: One Two Three Four Five" };
	  vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 
	int m = 2; 
	int n = 5; 
	string danceCall = "FiveParts"; 
	string RetVal_[] = {"One", "Two" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); }
int Test_(Case_<1>) {
	string definitions_[] = { "Trade: Hinge Hinge",
  "TradeThreeTimes: Trade Trade Trade" };
	  vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 
	int m = 5; 
	int n = 6; 
	string danceCall = "TradeThreeTimes"; 
	string RetVal_[] = {"Trade", "Trade", "Hinge" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); }
int Test_(Case_<2>) {
	string definitions_[] = { "Trade: Hinge Hinge",
  "CenterTwoTrade: CenterTwoHinge CenterTwoHinge",
  "SpinTheTop: Trade FanTheTop",
  "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" };
	  vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 
	int m = 1; 
	int n = 2; 
	string danceCall = "HotFootSpin"; 
	string RetVal_[] = {"FanTheTop", "CenterTwoHinge" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); }
int Test_(Case_<3>) {
	string definitions_[] = { "Trade: Hinge Hinge",
  "CenterTwoTrade: CenterTwoHinge CenterTwoHinge",
  "SpinTheTop: Trade FanTheTop",
  "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" };
	  vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 
	int m = 3; 
	int n = 4; 
	string danceCall = "HotFootSpin"; 
	string RetVal_[] = {"FanTheTop", "CenterTwoTrade", "Hinge" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); }
int Test_(Case_<4>) {
	string definitions_[] = { "A: One Two",
  "B: One Two Three",
  "C: One Two Three Four" };
	  vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 
	int m = 3; 
	int n = 4; 
	string danceCall = "C"; 
	string RetVal_[] = {"One", "Two", "Three" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); }
int Test_(Case_<5>) {
	string definitions_[] = { "A: X Y Z",
  "B: A A A" };
	  vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 
	int m = 7; 
	int n = 23; 
	string danceCall = "B"; 
	string RetVal_[] = {"ILLEGAL" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); }
int Test_(Case_<6>) {
	string definitions_[] = { "A: B C",
  "D: E F" };
	  vector <string> definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); 
	int m = 1; 
	int n = 2; 
	string danceCall = "G"; 
	string RetVal_[] = {"ILLEGAL" };
	  vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 
	return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE