Artifact Content
Not logged in

Artifact 5977a8407d0587720c0514f2ff29cc2f10b52fe4


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class DengklekGaneshAndChains { public:
	string getBestChains(vector<string> chains, vector<int> lengths)
	{
		for (string& chain : chains)
			chain = make_it_lex_last(chain);
		sort(chains.begin(), chains.end());

		string ans;
		for (int len : lengths)
		{
			string s = chains.back().substr(0, len);
			for (size_t i = 0; i < chains.size(); ++i)
				if(chains[i].substr(0, len) == s) {
					ans += s;
					chains.erase(chains.begin() + i);
					break;
				}
		}
		return ans;
	}

	string make_it_lex_last(const string& s)
	{
		string t = s;
		for (size_t i = 1; i < s.size(); ++i)
			t = max(t, s.substr(i) + s.substr(0, i));
		return t;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, DengklekGaneshAndChains().getBestChains(chains, lengths));}
int main(){

CASE(0)
	string chains_[] = {"topc", "oder", "open", "twob"};
	  vector <string> chains(chains_, chains_+sizeof(chains_)/sizeof(*chains_)); 
	int lengths_[] = {2, 1, 3};
	  vector <int> lengths(lengths_, lengths_+sizeof(lengths_)/sizeof(*lengths_)); 
	string _ = "wotrod"; 
END
CASE(1)
	string chains_[] = {"ssh", "she", "see", "sea"};
	  vector <string> chains(chains_, chains_+sizeof(chains_)/sizeof(*chains_)); 
	int lengths_[] = {2, 3, 2, 3};
	  vector <int> lengths(lengths_, lengths_+sizeof(lengths_)/sizeof(*lengths_)); 
	string _ = "ssshesesee"; 
END
CASE(2)
	string chains_[] = {"wri", "ter", "who", "are", "you"};
	  vector <string> chains(chains_, chains_+sizeof(chains_)/sizeof(*chains_)); 
	int lengths_[] = {3};
	  vector <int> lengths(lengths_, lengths_+sizeof(lengths_)/sizeof(*lengths_)); 
	string _ = "you"; 
END
CASE(3)
	string chains_[] = {"harus", "imfyo"};
	  vector <string> chains(chains_, chains_+sizeof(chains_)/sizeof(*chains_)); 
	int lengths_[] = {5, 5};
	  vector <int> lengths(lengths_, lengths_+sizeof(lengths_)/sizeof(*lengths_)); 
	string _ = "yoimfushar"; 
END
/*
CASE(4)
	string chains_[] = ;
	  vector <string> chains(chains_, chains_+sizeof(chains_)/sizeof(*chains_)); 
	int lengths_[] = ;
	  vector <int> lengths(lengths_, lengths_+sizeof(lengths_)/sizeof(*lengths_)); 
	string _ = ; 
END
CASE(5)
	string chains_[] = ;
	  vector <string> chains(chains_, chains_+sizeof(chains_)/sizeof(*chains_)); 
	int lengths_[] = ;
	  vector <int> lengths(lengths_, lengths_+sizeof(lengths_)/sizeof(*lengths_)); 
	string _ = ; 
END
*/
}
// END CUT HERE