1. Maximum Mapping
There are n strings, each of which is composed of the uppercase characters of A-j. Now you map each character to a 0-9 number, with different characters mapped to different numbers. Each string can then be treated as an integer, and the only requirement is that the integers must be positive integers and their strings cannot have leading zeros. Now how do you map characters to make the sum of the integers represented by these strings the largest?
Input Description:
Each set of test cases contains only one set of data, the first behavior of each group of data is a positive integer n, followed by n rows, each line a length of not more than 12 and contains only uppercase letter A-j string. n is not greater than 50, and there is at least one character that is not the first letter of any string.
Output Description:
Outputs a number that represents the maximum and how much.
Input Example:
2
Abc
Bca
Output Example:
1875
The code is as follows:
#include <iostream> #include <vector> #include <string> #include <algorithm> #include < numeric>//numeric algorithm using namespace std;//node class: Each character corresponds to a weight struct node{char ch;long long num; Node () {}//default constructor node (char c, int n): Ch (c), num (n) {}};//sorts the nodes by weight BOOL CMP (const node &a, const node &B) {return a.num < B.num;} Gets the weight of each character void Getweight (vector<string> &data, vector<node> &arr) {for (int i = 0; i < data.size () ; ++i) {Long Long index = 1;for (int j = data[i].size ()-1; J >= 0;--j) {int pos = data[i][j];arr[pos-' A '].num + = Index ; index *= 10;}}} int main () {int n;while (cin >> N) {string str;//string collection vector<string> data (n); for (int i = 0; i < n; ++i) {cin >> str;data[i] = str;} Node class collection vector<node> arr (int i = ' a '; I <= ' J '; ++i) {arr[i-' a '] = node (i, 0);} Calculates the weight of each character getweight (data, arr);//Sort by weight from small to large (Arr.begin (), Arr.end (), CMP);//Whether each character appears at the first position of the string (1: appeared) vector <int> flag (0); foR (int i = 0; i < n; ++i) {flag[data[i][0]-' A '] = 1;} In the sorted array, the first position of the character that does not appear at the first position of the string int spec_pos = 0;for (; spec_pos < ++spec_pos) {if (flag[arr[spec_pos].ch-' A '] = = 0) break;} Node tmp = arr[spec_pos];//moves backward one bit, placing elements of the Spec_pos position in the header for (int i = spec_pos; i > 0; i) {arr[i] = arr[i-1];} ARR[0] = tmp;//assigns the 0~9 to the corresponding character vector<int> numarr (int i = 0; i <; ++i) {numarr[arr[i].ch-' A '] = i;} The string collection is converted to an integer set of Vector<long long> num (n), for (int i = 0; i < n; ++i) {long Long val = 0;long Long index = 1;for (i NT J = data[i].size ()-1; J >= 0; --J) {val + = numarr[data[i][j]-' A '] * index;index *= 10;} Num[i] = val;} Long long, Count = 0;//evaluates the integral number set cout << accumulate (Num.begin (), Num.end (), count) << Endl;} return 0;}
Today's headline 2017 back-end engineer Pen questions