516f62332d 2012-10-10 kinaba: #include <iostream> 516f62332d 2012-10-10 kinaba: #include <sstream> 516f62332d 2012-10-10 kinaba: #include <iomanip> 516f62332d 2012-10-10 kinaba: #include <vector> 516f62332d 2012-10-10 kinaba: #include <string> 516f62332d 2012-10-10 kinaba: #include <map> 516f62332d 2012-10-10 kinaba: #include <set> 516f62332d 2012-10-10 kinaba: #include <algorithm> 516f62332d 2012-10-10 kinaba: #include <numeric> 516f62332d 2012-10-10 kinaba: #include <iterator> 516f62332d 2012-10-10 kinaba: #include <functional> 516f62332d 2012-10-10 kinaba: #include <complex> 516f62332d 2012-10-10 kinaba: #include <queue> 516f62332d 2012-10-10 kinaba: #include <stack> 516f62332d 2012-10-10 kinaba: #include <cmath> 516f62332d 2012-10-10 kinaba: #include <cassert> 516f62332d 2012-10-10 kinaba: using namespace std; 516f62332d 2012-10-10 kinaba: typedef long long LL; 516f62332d 2012-10-10 kinaba: typedef long double LD; 516f62332d 2012-10-10 kinaba: typedef complex<LD> CMP; 516f62332d 2012-10-10 kinaba: 516f62332d 2012-10-10 kinaba: class LeftRightDigitsGame { public: 516f62332d 2012-10-10 kinaba: string minNumber(string digits) 516f62332d 2012-10-10 kinaba: { 516f62332d 2012-10-10 kinaba: string cur, nonzero; 516f62332d 2012-10-10 kinaba: for(int i=0; i<digits.size(); ++i) { 516f62332d 2012-10-10 kinaba: if(digits[i] != '0') 516f62332d 2012-10-10 kinaba: if(!nonzero.empty() && nonzero[0]=='0') 516f62332d 2012-10-10 kinaba: nonzero = digits[i]+cur; 516f62332d 2012-10-10 kinaba: else 516f62332d 2012-10-10 kinaba: nonzero = min(digits[i]+cur, nonzero+digits[i]); 516f62332d 2012-10-10 kinaba: else 516f62332d 2012-10-10 kinaba: nonzero = nonzero+digits[i]; 516f62332d 2012-10-10 kinaba: cur = min(cur+digits[i], digits[i]+cur); 516f62332d 2012-10-10 kinaba: } 516f62332d 2012-10-10 kinaba: return nonzero; 516f62332d 2012-10-10 kinaba: } 516f62332d 2012-10-10 kinaba: }; 516f62332d 2012-10-10 kinaba: 516f62332d 2012-10-10 kinaba: // BEGIN CUT HERE 516f62332d 2012-10-10 kinaba: #include <ctime> 516f62332d 2012-10-10 kinaba: double start_time; string timer() 516f62332d 2012-10-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 516f62332d 2012-10-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 516f62332d 2012-10-10 kinaba: { os << "{ "; 516f62332d 2012-10-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 516f62332d 2012-10-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 516f62332d 2012-10-10 kinaba: void verify_case(const string& Expected, const string& Received) { 516f62332d 2012-10-10 kinaba: bool ok = (Expected == Received); 516f62332d 2012-10-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 516f62332d 2012-10-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 516f62332d 2012-10-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 516f62332d 2012-10-10 kinaba: #define END verify_case(_, LeftRightDigitsGame().minNumber(digits));} 516f62332d 2012-10-10 kinaba: int main(){ 516f62332d 2012-10-10 kinaba: 516f62332d 2012-10-10 kinaba: CASE(0) 516f62332d 2012-10-10 kinaba: string digits = "565"; 516f62332d 2012-10-10 kinaba: string _ = "556"; 516f62332d 2012-10-10 kinaba: END 516f62332d 2012-10-10 kinaba: CASE(1) 516f62332d 2012-10-10 kinaba: string digits = "9876543210"; 516f62332d 2012-10-10 kinaba: string _ = "1234567890"; 516f62332d 2012-10-10 kinaba: END 516f62332d 2012-10-10 kinaba: CASE(2) 516f62332d 2012-10-10 kinaba: string digits = "8016352"; 516f62332d 2012-10-10 kinaba: string _ = "1086352"; 516f62332d 2012-10-10 kinaba: END 516f62332d 2012-10-10 kinaba: CASE(3) 516f62332d 2012-10-10 kinaba: string digits = "01"; 516f62332d 2012-10-10 kinaba: string _ = "10"; 516f62332d 2012-10-10 kinaba: END 516f62332d 2012-10-10 kinaba: /* 516f62332d 2012-10-10 kinaba: CASE(4) 516f62332d 2012-10-10 kinaba: string digits = ; 516f62332d 2012-10-10 kinaba: string _ = ; 516f62332d 2012-10-10 kinaba: END 516f62332d 2012-10-10 kinaba: */ 516f62332d 2012-10-10 kinaba: } 516f62332d 2012-10-10 kinaba: // END CUT HERE