Artifact Content
Not logged in

Artifact 58cb8e25f4180cbedf8c8f355baf4b60e07b6b92


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class LeftRightDigitsGame2 { public:
	string minNumber(string digits, string lowerBound)
	{
		return rec(digits, lowerBound);
	}

	map<string, string> memo;
	string rec(const string& digits, const string& lwb)
	{
		if(lwb.size() == 0)
			return lwb;
		if(memo.count(lwb))
			return memo[lwb];

		string best = (digits.substr(0,lwb.size())>=lwb ? digits.substr(0,lwb.size()) : "");
		string minSoFar = string(1, digits[0]);
		for(int t=1; t<lwb.size(); ++t) {
			if( digits[t] < lwb[0] )
			{
			}
			else if( digits[t] == lwb[0] )
			{
				string tail = digits.substr(t+1, lwb.size()-(t+1));

				string target = lwb.substr(1, t);
				bool needOneMore = (lwb.substr(t+1) > tail);
				if(!needOneMore || incr(target)) {
					string m = minNumber(digits, target);
					if( m.size() == target.size() ) {
						string cand = digits[t] + m + tail;
						if( lwb<=cand && (best.empty() || best > cand) )
							best = cand;
					}
				}
			}
			else
			{
				string cand = digits[t] + minSoFar + digits.substr(t+1, lwb.size()-(t+1));
				if( best.empty() || best > cand )
					best = cand;
			}

			minSoFar = min(minSoFar+digits[t], digits[t]+minSoFar);
		}
		return memo[lwb] = best;
	}

	bool incr(string& s)
	{
		for(int i=s.size()-1 ;; --i)
		{
			if(i==-1)
				return false;
			if(s[i]<'9') {
				s[i]++;
				break;
			}
			s[i]='0';
		}
		return true;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, LeftRightDigitsGame2().minNumber(digits, lowerBound));}
int main(){

CASE(0)
	string digits = "565"; 
	string lowerBound = "556"; 
	string _ = "556"; 
END
CASE(1)
	string digits = "565"; 
	string lowerBound = "566"; 
	string _ = "655"; 
END
CASE(2)
	string digits = "565"; 
	string lowerBound = "656"; 
	string _ = ""; 
END
CASE(3)
	string digits = "9876543210"; 
	string lowerBound = "5565565565"; 
	string _ = "5678943210"; 
END
CASE(4)
	string digits = "8016352"; 
	string lowerBound = "1000000"; 
	string _ = "1086352"; 
END
CASE(5)
	string digits = "93907757041066207580227527897335194812859183833602"; 
	string lowerBound = "12089579919698667599337853067564506011831210501927"; 
	string _ = ""; 
END
CASE(6)
	string digits = "1"; 
	string lowerBound = "2"; 
	string _ = ""; 
END
CASE(6)
	string digits = "2"; 
	string lowerBound = "2"; 
	string _ = "2"; 
END
CASE(6)
	string digits = "3"; 
	string lowerBound = "2"; 
	string _ = "3"; 
END
CASE(6)
	string digits = "55555555555555555555555555555555555555555555555555"; 
	string lowerBound = "55555555555555555555555555555555555555555555555555"; 
	string _ = "55555555555555555555555555555555555555555555555555"; 
END
}
// END CUT HERE