Artifact Content
Not logged in

Artifact 22831069cfdf7e1fe2955b1f4d10f6b59d5c21fa


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

class GroupedWord {
public:
	string restore(vector <string> parts) 
	{
		while( reduce(parts) ) {}

		if( parts.size() == 1 )
			return gd(parts[0]) ? parts[0] : "IMPOSSIBLE";

		string s = accumulate(parts.begin(), parts.end(), string(""));
		return gd(s) ? "MANY" : "IMPOSSIBLE";
	}

	bool reduce( vector<string>& p )
	{
		for(int j=0; j<p.size(); ++j) {
			string& s = p[j];

			char last = s[s.size()-1];
			for(int i=0; i<p.size(); ++i) if(i!=j)
				if( count(p[i].begin(), p[i].end(), last) == p[i].size() )
				{
					p.push_back(s+p[i]);
					p.erase(p.begin()+max(i,j));
					p.erase(p.begin()+min(i,j));
					return true;
				}

			for(int i=0; i<p.size(); ++i) if(i!=j)
				if( p[i][0] == last )
				{
					p.push_back(s+p[i]);
					p.erase(p.begin()+max(i,j));
					p.erase(p.begin()+min(i,j));
					return true;
				}
		}
		return false;
	}

	bool gd(const string& s)
	{
		for(int i=0; i<s.size(); )
		{
			int j=i;
			while( j<s.size() && s[i]==s[j] ) ++j;
			if( s.find(s[i], j)!=string::npos )
				return false;
			i=j;
		}
		return true;
	}
};

// 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 string &Expected, const string &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string parts_[] = {"aaa", "a", "aa"};
	  vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 
	string RetVal = "aaaaaa"; 
	return verify_case(RetVal, GroupedWord().restore(parts)); }
int Test_(Case_<1>) {
	string parts_[] = {"ab", "bba"};
	  vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 
	string RetVal = "IMPOSSIBLE"; 
	return verify_case(RetVal, GroupedWord().restore(parts)); }
int Test_(Case_<2>) {
	string parts_[] = {"te", "st"};
	  vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 
	string RetVal = "stte"; 
	return verify_case(RetVal, GroupedWord().restore(parts)); }
int Test_(Case_<3>) {
	string parts_[] = {"te", "s", "t"};
	  vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 
	string RetVal = "MANY"; 
	return verify_case(RetVal, GroupedWord().restore(parts)); }
int Test_(Case_<4>) {
	string parts_[] = {"orr", "rd", "woo", "www"};
	  vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 
	string RetVal = "wwwwooorrrd"; 
	return verify_case(RetVal, GroupedWord().restore(parts)); }
int Test_(Case_<5>) {
	string parts_[] = {"abcb"};
	  vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 
	string RetVal = "IMPOSSIBLE"; 
	return verify_case(RetVal, GroupedWord().restore(parts)); }

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