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: d948683958 2012-10-10 kinaba: class LeftRightDigitsGame2 { public: d948683958 2012-10-10 kinaba: string minNumber(string digits, string lowerBound) d948683958 2012-10-10 kinaba: { d948683958 2012-10-10 kinaba: return rec(digits, lowerBound); d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: d948683958 2012-10-10 kinaba: map<string, string> memo; d948683958 2012-10-10 kinaba: string rec(const string& digits, const string& lwb) d948683958 2012-10-10 kinaba: { d948683958 2012-10-10 kinaba: if(lwb.size() == 0) d948683958 2012-10-10 kinaba: return lwb; d948683958 2012-10-10 kinaba: if(memo.count(lwb)) d948683958 2012-10-10 kinaba: return memo[lwb]; d948683958 2012-10-10 kinaba: d948683958 2012-10-10 kinaba: string best = (digits.substr(0,lwb.size())>=lwb ? digits.substr(0,lwb.size()) : ""); d948683958 2012-10-10 kinaba: string minSoFar = string(1, digits[0]); d948683958 2012-10-10 kinaba: for(int t=1; t<lwb.size(); ++t) { d948683958 2012-10-10 kinaba: if( digits[t] < lwb[0] ) d948683958 2012-10-10 kinaba: { d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: else if( digits[t] == lwb[0] ) d948683958 2012-10-10 kinaba: { d948683958 2012-10-10 kinaba: string tail = digits.substr(t+1, lwb.size()-(t+1)); d948683958 2012-10-10 kinaba: d948683958 2012-10-10 kinaba: string target = lwb.substr(1, t); d948683958 2012-10-10 kinaba: bool needOneMore = (lwb.substr(t+1) > tail); d948683958 2012-10-10 kinaba: if(!needOneMore || incr(target)) { d948683958 2012-10-10 kinaba: string m = minNumber(digits, target); d948683958 2012-10-10 kinaba: if( m.size() == target.size() ) { d948683958 2012-10-10 kinaba: string cand = digits[t] + m + tail; d948683958 2012-10-10 kinaba: if( lwb<=cand && (best.empty() || best > cand) ) d948683958 2012-10-10 kinaba: best = cand; d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: else d948683958 2012-10-10 kinaba: { d948683958 2012-10-10 kinaba: string cand = digits[t] + minSoFar + digits.substr(t+1, lwb.size()-(t+1)); d948683958 2012-10-10 kinaba: if( best.empty() || best > cand ) d948683958 2012-10-10 kinaba: best = cand; d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: d948683958 2012-10-10 kinaba: minSoFar = min(minSoFar+digits[t], digits[t]+minSoFar); d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: return memo[lwb] = best; d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: d948683958 2012-10-10 kinaba: bool incr(string& s) d948683958 2012-10-10 kinaba: { d948683958 2012-10-10 kinaba: for(int i=s.size()-1 ;; --i) d948683958 2012-10-10 kinaba: { d948683958 2012-10-10 kinaba: if(i==-1) d948683958 2012-10-10 kinaba: return false; d948683958 2012-10-10 kinaba: if(s[i]<'9') { d948683958 2012-10-10 kinaba: s[i]++; d948683958 2012-10-10 kinaba: break; d948683958 2012-10-10 kinaba: } d948683958 2012-10-10 kinaba: s[i]='0'; d948683958 2012-10-10 kinaba: } d948683958 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; } d948683958 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(); d948683958 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) d948683958 2012-10-10 kinaba: string digits = "565"; d948683958 2012-10-10 kinaba: string lowerBound = "556"; d948683958 2012-10-10 kinaba: string _ = "556"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(1) d948683958 2012-10-10 kinaba: string digits = "565"; d948683958 2012-10-10 kinaba: string lowerBound = "566"; d948683958 2012-10-10 kinaba: string _ = "655"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(2) d948683958 2012-10-10 kinaba: string digits = "565"; d948683958 2012-10-10 kinaba: string lowerBound = "656"; d948683958 2012-10-10 kinaba: string _ = ""; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(3) d948683958 2012-10-10 kinaba: string digits = "9876543210"; d948683958 2012-10-10 kinaba: string lowerBound = "5565565565"; d948683958 2012-10-10 kinaba: string _ = "5678943210"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(4) d948683958 2012-10-10 kinaba: string digits = "8016352"; d948683958 2012-10-10 kinaba: string lowerBound = "1000000"; d948683958 2012-10-10 kinaba: string _ = "1086352"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(5) d948683958 2012-10-10 kinaba: string digits = "93907757041066207580227527897335194812859183833602"; d948683958 2012-10-10 kinaba: string lowerBound = "12089579919698667599337853067564506011831210501927"; d948683958 2012-10-10 kinaba: string _ = ""; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(6) d948683958 2012-10-10 kinaba: string digits = "1"; d948683958 2012-10-10 kinaba: string lowerBound = "2"; d948683958 2012-10-10 kinaba: string _ = ""; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(6) d948683958 2012-10-10 kinaba: string digits = "2"; d948683958 2012-10-10 kinaba: string lowerBound = "2"; d948683958 2012-10-10 kinaba: string _ = "2"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(6) d948683958 2012-10-10 kinaba: string digits = "3"; d948683958 2012-10-10 kinaba: string lowerBound = "2"; d948683958 2012-10-10 kinaba: string _ = "3"; d948683958 2012-10-10 kinaba: END d948683958 2012-10-10 kinaba: CASE(6) d948683958 2012-10-10 kinaba: string digits = "55555555555555555555555555555555555555555555555555"; d948683958 2012-10-10 kinaba: string lowerBound = "55555555555555555555555555555555555555555555555555"; d948683958 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