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 <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: 4fd800b3a8 2011-02-23 kinaba: class GroupedWord { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: string restore(vector <string> parts) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: while( reduce(parts) ) {} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( parts.size() == 1 ) 4fd800b3a8 2011-02-23 kinaba: return gd(parts[0]) ? parts[0] : "IMPOSSIBLE"; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: string s = accumulate(parts.begin(), parts.end(), string("")); 4fd800b3a8 2011-02-23 kinaba: return gd(s) ? "MANY" : "IMPOSSIBLE"; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool reduce( vector<string>& p ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<p.size(); ++j) { 4fd800b3a8 2011-02-23 kinaba: string& s = p[j]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: char last = s[s.size()-1]; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<p.size(); ++i) if(i!=j) 4fd800b3a8 2011-02-23 kinaba: if( count(p[i].begin(), p[i].end(), last) == p[i].size() ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: p.push_back(s+p[i]); 4fd800b3a8 2011-02-23 kinaba: p.erase(p.begin()+max(i,j)); 4fd800b3a8 2011-02-23 kinaba: p.erase(p.begin()+min(i,j)); 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<p.size(); ++i) if(i!=j) 4fd800b3a8 2011-02-23 kinaba: if( p[i][0] == last ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: p.push_back(s+p[i]); 4fd800b3a8 2011-02-23 kinaba: p.erase(p.begin()+max(i,j)); 4fd800b3a8 2011-02-23 kinaba: p.erase(p.begin()+min(i,j)); 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool gd(const string& s) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<s.size(); ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int j=i; 4fd800b3a8 2011-02-23 kinaba: while( j<s.size() && s[i]==s[j] ) ++j; 4fd800b3a8 2011-02-23 kinaba: if( s.find(s[i], j)!=string::npos ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: i=j; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return true; 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 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;} 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 parts_[] = {"aaa", "a", "aa"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal = "aaaaaa"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GroupedWord().restore(parts)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string parts_[] = {"ab", "bba"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal = "IMPOSSIBLE"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GroupedWord().restore(parts)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string parts_[] = {"te", "st"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal = "stte"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GroupedWord().restore(parts)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string parts_[] = {"te", "s", "t"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal = "MANY"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GroupedWord().restore(parts)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string parts_[] = {"orr", "rd", "woo", "www"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal = "wwwwooorrrd"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GroupedWord().restore(parts)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string parts_[] = {"abcb"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); 4fd800b3a8 2011-02-23 kinaba: string RetVal = "IMPOSSIBLE"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, GroupedWord().restore(parts)); } 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: