https://www.acmicpc.net/problem/1339
※ next_permutation은 내림차순으로 순열을 만드므로, sort로 미리 정렬해주는 것을 잊지말자
10
ABB
BB
BB
BB
BB
BB
BB
BB
BB
BB
정답 : 1790
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> words;
vector<char> word;
map<char, int> match;
int num = 9;
for (int i = 0; i < n; i++) {
string input;
cin >> input;
words.push_back(input);
for (int j = 0; j < input.length(); j++) {
if (find(word.begin(), word.end(), input[j]) == word.end()) {
word.push_back(input[j]);
}
}
}
sort(word.begin(), word.end());
int ret = 0;
do {
for (int i=0; i <word.size(); i++) {
if (word[i] != NULL) match[word[i]] = i+(10-word.size());
else match[word[i]] = 0;
}
int sum = 0;
for (int i = 0; i < n; i++) {
int tem = 0;
for (int j = 0; j < words[i].length(); j++) {
tem = tem * 10 + match[words[i][j]];
}
sum += tem;
}
ret = max(ret, sum);
} while (next_permutation(word.begin(), word.end()));
cout << ret << "\n";
return 0;
}