148e3f363b 2011-09-10 kinaba: #include <iostream> 148e3f363b 2011-09-10 kinaba: #include <sstream> 148e3f363b 2011-09-10 kinaba: #include <iomanip> 148e3f363b 2011-09-10 kinaba: #include <vector> 148e3f363b 2011-09-10 kinaba: #include <string> 148e3f363b 2011-09-10 kinaba: #include <map> 148e3f363b 2011-09-10 kinaba: #include <set> 148e3f363b 2011-09-10 kinaba: #include <algorithm> 148e3f363b 2011-09-10 kinaba: #include <numeric> 148e3f363b 2011-09-10 kinaba: #include <iterator> 148e3f363b 2011-09-10 kinaba: #include <functional> 148e3f363b 2011-09-10 kinaba: #include <complex> 148e3f363b 2011-09-10 kinaba: #include <queue> 148e3f363b 2011-09-10 kinaba: #include <stack> 148e3f363b 2011-09-10 kinaba: #include <cmath> 148e3f363b 2011-09-10 kinaba: #include <cassert> 148e3f363b 2011-09-10 kinaba: #include <cstring> 148e3f363b 2011-09-10 kinaba: using namespace std; 148e3f363b 2011-09-10 kinaba: typedef long long LL; 148e3f363b 2011-09-10 kinaba: typedef complex<double> CMP; 148e3f363b 2011-09-10 kinaba: 148e3f363b 2011-09-10 kinaba: class NetworkXMessageRecovery { public: 148e3f363b 2011-09-10 kinaba: string recover(vector <string> messages) 148e3f363b 2011-09-10 kinaba: { 148e3f363b 2011-09-10 kinaba: map< char, set<char> > prev; 148e3f363b 2011-09-10 kinaba: for(int i=0; i<messages.size(); ++i) { 148e3f363b 2011-09-10 kinaba: prev[messages[i][0]]; 148e3f363b 2011-09-10 kinaba: for(int j=0; j+1<messages[i].size(); ++j) 148e3f363b 2011-09-10 kinaba: prev[messages[i][j+1]].insert(messages[i][j]); 148e3f363b 2011-09-10 kinaba: } 148e3f363b 2011-09-10 kinaba: 148e3f363b 2011-09-10 kinaba: string result; 148e3f363b 2011-09-10 kinaba: while( !prev.empty() ) 148e3f363b 2011-09-10 kinaba: { 148e3f363b 2011-09-10 kinaba: set<char> cand; 148e3f363b 2011-09-10 kinaba: for(map< char,set<char> >::iterator it=prev.begin(); it!=prev.end(); ++it) 148e3f363b 2011-09-10 kinaba: if( it->second.empty() ) 148e3f363b 2011-09-10 kinaba: cand.insert(it->first); 148e3f363b 2011-09-10 kinaba: char c = *cand.begin(); 148e3f363b 2011-09-10 kinaba: result += c; 148e3f363b 2011-09-10 kinaba: for(map< char,set<char> >::iterator it=prev.begin(); it!=prev.end(); ++it) 148e3f363b 2011-09-10 kinaba: it->second.erase(c); 148e3f363b 2011-09-10 kinaba: prev.erase(c); 148e3f363b 2011-09-10 kinaba: } 148e3f363b 2011-09-10 kinaba: return result; 148e3f363b 2011-09-10 kinaba: } 148e3f363b 2011-09-10 kinaba: }; 148e3f363b 2011-09-10 kinaba: 148e3f363b 2011-09-10 kinaba: // BEGIN CUT HERE 148e3f363b 2011-09-10 kinaba: #include <ctime> 148e3f363b 2011-09-10 kinaba: double start_time; string timer() 148e3f363b 2011-09-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 148e3f363b 2011-09-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 148e3f363b 2011-09-10 kinaba: { os << "{ "; 148e3f363b 2011-09-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 148e3f363b 2011-09-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 148e3f363b 2011-09-10 kinaba: void verify_case(const string& Expected, const string& Received) { 148e3f363b 2011-09-10 kinaba: bool ok = (Expected == Received); 148e3f363b 2011-09-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 148e3f363b 2011-09-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 148e3f363b 2011-09-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 148e3f363b 2011-09-10 kinaba: #define END verify_case(_, NetworkXMessageRecovery().recover(messages));} 148e3f363b 2011-09-10 kinaba: int main(){ 148e3f363b 2011-09-10 kinaba: 148e3f363b 2011-09-10 kinaba: CASE(0) 148e3f363b 2011-09-10 kinaba: string messages_[] = {"acd", "bce"}; 148e3f363b 2011-09-10 kinaba: vector <string> messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); 148e3f363b 2011-09-10 kinaba: string _ = "abcde"; 148e3f363b 2011-09-10 kinaba: END 148e3f363b 2011-09-10 kinaba: CASE(1) 148e3f363b 2011-09-10 kinaba: string messages_[] = {"ed", "dc", "cb", "ba"}; 148e3f363b 2011-09-10 kinaba: vector <string> messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); 148e3f363b 2011-09-10 kinaba: string _ = "edcba"; 148e3f363b 2011-09-10 kinaba: END 148e3f363b 2011-09-10 kinaba: CASE(2) 148e3f363b 2011-09-10 kinaba: string messages_[] = {"Fox", "Ciel", "FoxCiel"}; 148e3f363b 2011-09-10 kinaba: vector <string> messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); 148e3f363b 2011-09-10 kinaba: string _ = "FoxCiel"; 148e3f363b 2011-09-10 kinaba: END 148e3f363b 2011-09-10 kinaba: CASE(3) 148e3f363b 2011-09-10 kinaba: string messages_[] = {"a", "A"}; 148e3f363b 2011-09-10 kinaba: vector <string> messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); 148e3f363b 2011-09-10 kinaba: string _ = "Aa"; 148e3f363b 2011-09-10 kinaba: END 148e3f363b 2011-09-10 kinaba: /* 148e3f363b 2011-09-10 kinaba: CASE(4) 148e3f363b 2011-09-10 kinaba: string messages_[] = ; 148e3f363b 2011-09-10 kinaba: vector <string> messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); 148e3f363b 2011-09-10 kinaba: string _ = ; 148e3f363b 2011-09-10 kinaba: END 148e3f363b 2011-09-10 kinaba: CASE(5) 148e3f363b 2011-09-10 kinaba: string messages_[] = ; 148e3f363b 2011-09-10 kinaba: vector <string> messages(messages_, messages_+sizeof(messages_)/sizeof(*messages_)); 148e3f363b 2011-09-10 kinaba: string _ = ; 148e3f363b 2011-09-10 kinaba: END 148e3f363b 2011-09-10 kinaba: */ 148e3f363b 2011-09-10 kinaba: } 148e3f363b 2011-09-10 kinaba: // END CUT HERE