fd58d23e5f 2012-12-12 kinaba: #include <iostream> fd58d23e5f 2012-12-12 kinaba: #include <sstream> fd58d23e5f 2012-12-12 kinaba: #include <iomanip> fd58d23e5f 2012-12-12 kinaba: #include <vector> fd58d23e5f 2012-12-12 kinaba: #include <string> fd58d23e5f 2012-12-12 kinaba: #include <map> fd58d23e5f 2012-12-12 kinaba: #include <set> fd58d23e5f 2012-12-12 kinaba: #include <algorithm> fd58d23e5f 2012-12-12 kinaba: #include <numeric> fd58d23e5f 2012-12-12 kinaba: #include <iterator> fd58d23e5f 2012-12-12 kinaba: #include <functional> fd58d23e5f 2012-12-12 kinaba: #include <complex> fd58d23e5f 2012-12-12 kinaba: #include <queue> fd58d23e5f 2012-12-12 kinaba: #include <stack> fd58d23e5f 2012-12-12 kinaba: #include <cmath> fd58d23e5f 2012-12-12 kinaba: #include <cassert> fd58d23e5f 2012-12-12 kinaba: using namespace std; fd58d23e5f 2012-12-12 kinaba: typedef long long LL; fd58d23e5f 2012-12-12 kinaba: typedef long double LD; fd58d23e5f 2012-12-12 kinaba: typedef complex<LD> CMP; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: class FoxAndHandle { public: fd58d23e5f 2012-12-12 kinaba: string lexSmallestName(string S) fd58d23e5f 2012-12-12 kinaba: { fd58d23e5f 2012-12-12 kinaba: string cur = ""; fd58d23e5f 2012-12-12 kinaba: int p = 0; fd58d23e5f 2012-12-12 kinaba: while(cur.size()*2<S.size()) { fd58d23e5f 2012-12-12 kinaba: string next_best; fd58d23e5f 2012-12-12 kinaba: int next_p = -1; fd58d23e5f 2012-12-12 kinaba: for(int x=p; x<S.size(); ++x) fd58d23e5f 2012-12-12 kinaba: if(possible(cur+S[x], x+1, S)) { fd58d23e5f 2012-12-12 kinaba: if(next_p==-1 || next_best>cur+S[x]) fd58d23e5f 2012-12-12 kinaba: next_best=cur+S[x], next_p=x+1; fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: cur = next_best; fd58d23e5f 2012-12-12 kinaba: p = next_p; fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: return cur; fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: bool possible(const string& used, int x, const string& S) fd58d23e5f 2012-12-12 kinaba: { fd58d23e5f 2012-12-12 kinaba: int freq[26] = {0}; fd58d23e5f 2012-12-12 kinaba: int pos[26] = {0}; fd58d23e5f 2012-12-12 kinaba: int tot[26] = {0}; fd58d23e5f 2012-12-12 kinaba: for(int i=0; i<used.size(); ++i) fd58d23e5f 2012-12-12 kinaba: freq[used[i]-'a']++; fd58d23e5f 2012-12-12 kinaba: for(int i=0; i<S.size(); ++i) { fd58d23e5f 2012-12-12 kinaba: if(x<=i) fd58d23e5f 2012-12-12 kinaba: pos[S[i]-'a']++; fd58d23e5f 2012-12-12 kinaba: tot[S[i]-'a']++; fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: for(int c=0; c<26; ++c) fd58d23e5f 2012-12-12 kinaba: if((freq[c]+pos[c])*2<tot[c] || freq[c]*2>tot[c]) fd58d23e5f 2012-12-12 kinaba: return false; fd58d23e5f 2012-12-12 kinaba: return true; fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: }; fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: // BEGIN CUT HERE fd58d23e5f 2012-12-12 kinaba: #include <ctime> fd58d23e5f 2012-12-12 kinaba: double start_time; string timer() fd58d23e5f 2012-12-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } fd58d23e5f 2012-12-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) fd58d23e5f 2012-12-12 kinaba: { os << "{ "; fd58d23e5f 2012-12-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) fd58d23e5f 2012-12-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } fd58d23e5f 2012-12-12 kinaba: void verify_case(const string& Expected, const string& Received) { fd58d23e5f 2012-12-12 kinaba: bool ok = (Expected == Received); fd58d23e5f 2012-12-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; fd58d23e5f 2012-12-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } fd58d23e5f 2012-12-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); fd58d23e5f 2012-12-12 kinaba: #define END verify_case(_, FoxAndHandle().lexSmallestName(S));} fd58d23e5f 2012-12-12 kinaba: int main(){ fd58d23e5f 2012-12-12 kinaba: fd58d23e5f 2012-12-12 kinaba: CASE(0) fd58d23e5f 2012-12-12 kinaba: string S = "foxfox"; fd58d23e5f 2012-12-12 kinaba: string _ = "fox"; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(1) fd58d23e5f 2012-12-12 kinaba: string S = "ccieliel"; fd58d23e5f 2012-12-12 kinaba: string _ = "ceil"; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(2) fd58d23e5f 2012-12-12 kinaba: string S = "abaabbab"; fd58d23e5f 2012-12-12 kinaba: string _ = "aabb"; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(3) fd58d23e5f 2012-12-12 kinaba: string S = "bbbbaaaa"; fd58d23e5f 2012-12-12 kinaba: string _ = "bbaa"; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(4) fd58d23e5f 2012-12-12 kinaba: string S = "fedcbafedcba"; fd58d23e5f 2012-12-12 kinaba: string _ = "afedcb"; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(5) fd58d23e5f 2012-12-12 kinaba: string S = "nodevillivedon"; fd58d23e5f 2012-12-12 kinaba: string _ = "deilvon"; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: /* fd58d23e5f 2012-12-12 kinaba: CASE(6) fd58d23e5f 2012-12-12 kinaba: string S = ; fd58d23e5f 2012-12-12 kinaba: string _ = ; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: CASE(7) fd58d23e5f 2012-12-12 kinaba: string S = ; fd58d23e5f 2012-12-12 kinaba: string _ = ; fd58d23e5f 2012-12-12 kinaba: END fd58d23e5f 2012-12-12 kinaba: */ fd58d23e5f 2012-12-12 kinaba: } fd58d23e5f 2012-12-12 kinaba: // END CUT HERE