Artifact Content
Not logged in

Artifact c544017c1bc2bc01f3adf8cd2ab610f4b727e6ce


#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>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class RemovingDigits { public:
	string maxNumber(string number, string digits) 
	{
		vector<int> d(9);
		for(int i=0; i<digits.size(); ++i)
			d[digits[i]-'1'] ++;
		vector<int> n(9);
		for(int i=0; i<number.size(); ++i)
			n[number[i]-'1'] ++;
		return maximize(number, 0, n, d);
	}

	map<pair< int, vector<int> >, string> memo;

	string maximize(const string& number, int s, vector<int>& n, vector<int>& d)
	{
		if( s == number.size() )
			return "";

		pair< int, vector<int> > key(s, d);
		if( memo.count(key) )
			return memo[key];

		char ch = number[s]-'1';
		string cand = "";

		n[ch]--;
		if( n[ch] >= d[ch] )
		{
			string c1 = number[s] + maximize(number, s+1, n, d);
			cand = max(cand, c1);
		}
		if( d[ch] > 0 )
		{
			d[ch]--;
			string c2 = maximize(number, s+1, n, d);
			cand = max(cand, c2);
			d[ch]++;
		}
		n[ch]++;
		return memo[key] = cand;
	}
};

// 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(_, RemovingDigits().maxNumber(number, digits));}
int main(){

CASE(0)
	string number = "12345"; 
	string digits = "513"; 
	string _ = "24"; 
END
CASE(1)
	string number = "112352"; 
	string digits = "1123"; 
	string _ = "52"; 
END
CASE(2)
	string number = "123456654321"; 
	string digits = "612534"; 
	string _ = "654321"; 
END
CASE(3)
	string number = "654321123456"; 
	string digits = "612534"; 
	string _ = "654321"; 
END
CASE(4)
	string number = "2654982765982365"; 
	string digits = "2345978"; 
	string _ = "698265265"; 
END
/*
CASE(5)
	string number = ; 
	string digits = ; 
	string _ = ; 
END
CASE(6)
	string number = ; 
	string digits = ; 
	string _ = ; 
END
*/
}
// END CUT HERE