d948683958 2012-10-10 kinaba: #include <iostream> d948683958 2012-10-10 kinaba: #include <sstream> d948683958 2012-10-10 kinaba: #include <iomanip> d948683958 2012-10-10 kinaba: #include <vector> d948683958 2012-10-10 kinaba: #include <string> d948683958 2012-10-10 kinaba: #include <map> d948683958 2012-10-10 kinaba: #include <set> d948683958 2012-10-10 kinaba: #include <algorithm> d948683958 2012-10-10 kinaba: #include <numeric> d948683958 2012-10-10 kinaba: #include <iterator> d948683958 2012-10-10 kinaba: #include <functional> d948683958 2012-10-10 kinaba: #include <complex> d948683958 2012-10-10 kinaba: #include <queue> d948683958 2012-10-10 kinaba: #include <stack> d948683958 2012-10-10 kinaba: #include <cmath> d948683958 2012-10-10 kinaba: #include <cassert> d948683958 2012-10-10 kinaba: using namespace std; d948683958 2012-10-10 kinaba: typedef long long LL; d948683958 2012-10-10 kinaba: typedef long double LD; d948683958 2012-10-10 kinaba: typedef complex<LD> CMP; d948683958 2012-10-10 kinaba: efcf46f57b 2012-10-10 kinaba: class LeftRightDigitsGame2 { public: efcf46f57b 2012-10-10 kinaba: string minNumber(string digits, string lowerBound) efcf46f57b 2012-10-10 kinaba: { efcf46f57b 2012-10-10 kinaba: return rec(digits, lowerBound); efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: efcf46f57b 2012-10-10 kinaba: map<string, string> memo; efcf46f57b 2012-10-10 kinaba: string rec(const string& digits, const string& lwb) d948683958 2012-10-10 kinaba: { efcf46f57b 2012-10-10 kinaba: if(lwb.size() == 0) efcf46f57b 2012-10-10 kinaba: return lwb; efcf46f57b 2012-10-10 kinaba: if(memo.count(lwb)) efcf46f57b 2012-10-10 kinaba: return memo[lwb]; efcf46f57b 2012-10-10 kinaba: efcf46f57b 2012-10-10 kinaba: string best = (digits.substr(0,lwb.size())>=lwb ? digits.substr(0,lwb.size()) : ""); efcf46f57b 2012-10-10 kinaba: string minSoFar = string(1, digits[0]); efcf46f57b 2012-10-10 kinaba: for(int t=1; t<lwb.size(); ++t) { efcf46f57b 2012-10-10 kinaba: if( digits[t] < lwb[0] ) efcf46f57b 2012-10-10 kinaba: { efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: else if( digits[t] == lwb[0] ) efcf46f57b 2012-10-10 kinaba: { efcf46f57b 2012-10-10 kinaba: string tail = digits.substr(t+1, lwb.size()-(t+1)); efcf46f57b 2012-10-10 kinaba: efcf46f57b 2012-10-10 kinaba: string target = lwb.substr(1, t); efcf46f57b 2012-10-10 kinaba: bool needOneMore = (lwb.substr(t+1) > tail); efcf46f57b 2012-10-10 kinaba: if(!needOneMore || incr(target)) { efcf46f57b 2012-10-10 kinaba: string m = minNumber(digits, target); efcf46f57b 2012-10-10 kinaba: if( m.size() == target.size() ) { efcf46f57b 2012-10-10 kinaba: string cand = digits[t] + m + tail; efcf46f57b 2012-10-10 kinaba: if( lwb<=cand && (best.empty() || best > cand) ) efcf46f57b 2012-10-10 kinaba: best = cand; efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: else efcf46f57b 2012-10-10 kinaba: { efcf46f57b 2012-10-10 kinaba: string cand = digits[t] + minSoFar + digits.substr(t+1, lwb.size()-(t+1)); efcf46f57b 2012-10-10 kinaba: if( best.empty() || best > cand ) efcf46f57b 2012-10-10 kinaba: best = cand; efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: efcf46f57b 2012-10-10 kinaba: minSoFar = min(minSoFar+digits[t], digits[t]+minSoFar); efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: return memo[lwb] = best; d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: efcf46f57b 2012-10-10 kinaba: bool incr(string& s) d948683958 2012-10-10 kinaba: { efcf46f57b 2012-10-10 kinaba: for(int i=s.size()-1 ;; --i) efcf46f57b 2012-10-10 kinaba: { efcf46f57b 2012-10-10 kinaba: if(i==-1) efcf46f57b 2012-10-10 kinaba: return false; efcf46f57b 2012-10-10 kinaba: if(s[i]<'9') { efcf46f57b 2012-10-10 kinaba: s[i]++; efcf46f57b 2012-10-10 kinaba: break; efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: s[i]='0'; efcf46f57b 2012-10-10 kinaba: } efcf46f57b 2012-10-10 kinaba: return true; d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: }; d948683958 2012-10-10 kinaba: d948683958 2012-10-10 kinaba: // BEGIN CUT HERE d948683958 2012-10-10 kinaba: #include <ctime> d948683958 2012-10-10 kinaba: double start_time; string timer() d948683958 2012-10-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d948683958 2012-10-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d948683958 2012-10-10 kinaba: { os << "{ "; d948683958 2012-10-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d948683958 2012-10-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } efcf46f57b 2012-10-10 kinaba: void verify_case(const string& Expected, const string& Received) { d948683958 2012-10-10 kinaba: bool ok = (Expected == Received); d948683958 2012-10-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d948683958 2012-10-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d948683958 2012-10-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); efcf46f57b 2012-10-10 kinaba: #define END verify_case(_, LeftRightDigitsGame2().minNumber(digits, lowerBound));} d948683958 2012-10-10 kinaba: int main(){ d948683958 2012-10-10 kinaba: d948683958 2012-10-10 kinaba: CASE(0) efcf46f57b 2012-10-10 kinaba: string digits = "565"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "556"; efcf46f57b 2012-10-10 kinaba: string _ = "556"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(1) efcf46f57b 2012-10-10 kinaba: string digits = "565"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "566"; efcf46f57b 2012-10-10 kinaba: string _ = "655"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(2) efcf46f57b 2012-10-10 kinaba: string digits = "565"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "656"; efcf46f57b 2012-10-10 kinaba: string _ = ""; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(3) efcf46f57b 2012-10-10 kinaba: string digits = "9876543210"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "5565565565"; efcf46f57b 2012-10-10 kinaba: string _ = "5678943210"; efcf46f57b 2012-10-10 kinaba: END efcf46f57b 2012-10-10 kinaba: CASE(4) efcf46f57b 2012-10-10 kinaba: string digits = "8016352"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "1000000"; efcf46f57b 2012-10-10 kinaba: string _ = "1086352"; efcf46f57b 2012-10-10 kinaba: END efcf46f57b 2012-10-10 kinaba: CASE(5) efcf46f57b 2012-10-10 kinaba: string digits = "93907757041066207580227527897335194812859183833602"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "12089579919698667599337853067564506011831210501927"; efcf46f57b 2012-10-10 kinaba: string _ = ""; efcf46f57b 2012-10-10 kinaba: END efcf46f57b 2012-10-10 kinaba: CASE(6) efcf46f57b 2012-10-10 kinaba: string digits = "1"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "2"; efcf46f57b 2012-10-10 kinaba: string _ = ""; efcf46f57b 2012-10-10 kinaba: END efcf46f57b 2012-10-10 kinaba: CASE(6) efcf46f57b 2012-10-10 kinaba: string digits = "2"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "2"; efcf46f57b 2012-10-10 kinaba: string _ = "2"; efcf46f57b 2012-10-10 kinaba: END efcf46f57b 2012-10-10 kinaba: CASE(6) efcf46f57b 2012-10-10 kinaba: string digits = "3"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "2"; efcf46f57b 2012-10-10 kinaba: string _ = "3"; d948683958 2012-10-10 kinaba: END efcf46f57b 2012-10-10 kinaba: CASE(6) efcf46f57b 2012-10-10 kinaba: string digits = "55555555555555555555555555555555555555555555555555"; efcf46f57b 2012-10-10 kinaba: string lowerBound = "55555555555555555555555555555555555555555555555555"; efcf46f57b 2012-10-10 kinaba: string _ = "55555555555555555555555555555555555555555555555555"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: // END CUT HERE