4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class EncodedSum 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: long long maximumSum(vector <string> numbers) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set<int> nonzero; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<numbers.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: nonzero.insert( numbers[i][0]-'A' ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL v[] = {0,0,0,0,0,0,0,0,0,0}; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<numbers.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(LL j=numbers[i].size()-1,d=1; j>=0; --j,d*=10) 4fd800b3a8 2011-02-23 kinaba: v[numbers[i][j]-'A'] += d; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL d[] = {0,1,2,3,4,5,6,7,8,9}; 4fd800b3a8 2011-02-23 kinaba: LL sumMax = 0; 4fd800b3a8 2011-02-23 kinaba: do { 4fd800b3a8 2011-02-23 kinaba: LL sum = 0; 4fd800b3a8 2011-02-23 kinaba: for(int c=0; c<10; ++c) 4fd800b3a8 2011-02-23 kinaba: if( d[c]==0 && nonzero.count(c) ) 4fd800b3a8 2011-02-23 kinaba: goto nextPerm; 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: sum += v[c] * d[c]; 4fd800b3a8 2011-02-23 kinaba: sumMax = max(sumMax, sum); 4fd800b3a8 2011-02-23 kinaba: nextPerm:; 4fd800b3a8 2011-02-23 kinaba: } while( next_permutation(d+0, d+10) ); 4fd800b3a8 2011-02-23 kinaba: return sumMax; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"ABC", 4fd800b3a8 2011-02-23 kinaba: "BCA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 1875LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"ABCDEFGHIJ"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 9876543210LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"ABCDEFGHIJ", 4fd800b3a8 2011-02-23 kinaba: "J"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 9876543202LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"A", 4fd800b3a8 2011-02-23 kinaba: "BB", 4fd800b3a8 2011-02-23 kinaba: "CCC", 4fd800b3a8 2011-02-23 kinaba: "DDDD", 4fd800b3a8 2011-02-23 kinaba: "EEEEE", 4fd800b3a8 2011-02-23 kinaba: "FFFFFF", 4fd800b3a8 2011-02-23 kinaba: "GGGGGGG", 4fd800b3a8 2011-02-23 kinaba: "HHHHHHHH", 4fd800b3a8 2011-02-23 kinaba: "IIIIIIIII", 4fd800b3a8 2011-02-23 kinaba: "AJJJJJJJJJ"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 9973936905LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"GHJIDDD", 4fd800b3a8 2011-02-23 kinaba: "AHHCCCA", 4fd800b3a8 2011-02-23 kinaba: "IIJCEJ", 4fd800b3a8 2011-02-23 kinaba: "F", 4fd800b3a8 2011-02-23 kinaba: "HDBIGFJAAJ"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 9888114550LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: