Artifact Content
Not logged in

Artifact aed588e5efe51737f6ec689f78fdef004ab89568


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

class EncodedSum
{
public:
	long long maximumSum(vector <string> numbers) 
	{
		set<int> nonzero;
		for(int i=0; i<numbers.size(); ++i)
			nonzero.insert( numbers[i][0]-'A' );

		LL v[] = {0,0,0,0,0,0,0,0,0,0};
		for(int i=0; i<numbers.size(); ++i)
			for(LL j=numbers[i].size()-1,d=1; j>=0; --j,d*=10)
				v[numbers[i][j]-'A'] += d;

		LL d[] = {0,1,2,3,4,5,6,7,8,9};
		LL sumMax = 0;
		do {
			LL sum = 0;
			for(int c=0; c<10; ++c)
				if( d[c]==0 && nonzero.count(c) )
					goto nextPerm;
				else
					sum += v[c] * d[c];
			sumMax = max(sumMax, sum);
		nextPerm:;
		} while( next_permutation(d+0, d+10) );
		return sumMax;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const long long &Expected, const long long &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string numbers_[] = {"ABC",
 "BCA"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	long long RetVal = 1875LL; 
	return verify_case(RetVal, EncodedSum().maximumSum(numbers)); }
int Test_(Case_<1>) {
	string numbers_[] = {"ABCDEFGHIJ"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	long long RetVal = 9876543210LL; 
	return verify_case(RetVal, EncodedSum().maximumSum(numbers)); }
int Test_(Case_<2>) {
	string numbers_[] = {"ABCDEFGHIJ", 
 "J"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	long long RetVal = 9876543202LL; 
	return verify_case(RetVal, EncodedSum().maximumSum(numbers)); }
int Test_(Case_<3>) {
	string numbers_[] = {"A", 
 "BB", 
 "CCC", 
 "DDDD", 
 "EEEEE", 
 "FFFFFF", 
 "GGGGGGG", 
 "HHHHHHHH", 
 "IIIIIIIII", 
 "AJJJJJJJJJ"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	long long RetVal = 9973936905LL; 
	return verify_case(RetVal, EncodedSum().maximumSum(numbers)); }
int Test_(Case_<4>) {
	string numbers_[] = {"GHJIDDD",
 "AHHCCCA",
 "IIJCEJ",
 "F",
 "HDBIGFJAAJ"};
	  vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 
	long long RetVal = 9888114550LL; 
	return verify_case(RetVal, EncodedSum().maximumSum(numbers)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE