μκ³ λ¦¬μ¦
λ°±μ€_1339_λ¨μ΄μν
shj718
2022. 5. 10. 07:39
πλ¬Έμ : https://www.acmicpc.net/problem/1339
1339λ²: λ¨μ΄ μν
첫째 μ€μ λ¨μ΄μ κ°μ N(1 ≤ N ≤ 10)μ΄ μ£Όμ΄μ§λ€. λμ§Έ μ€λΆν° Nκ°μ μ€μ λ¨μ΄κ° ν μ€μ νλμ© μ£Όμ΄μ§λ€. λ¨μ΄λ μνλ²³ λλ¬Έμλ‘λ§ μ΄λ£¨μ΄μ Έμλ€. λͺ¨λ λ¨μ΄μ ν¬ν¨λμ΄ μλ μνλ²³μ μ΅λ
www.acmicpc.net
πμκ³ λ¦¬μ¦: λΈλ£¨νΈν¬μ€ - μμ΄ (Greedy λλ λΈλ£¨νΈν¬μ€-μ¬κ·λ‘λ ν μ μμ)
πμμ΄λμ΄: ASCII μ½λλ₯Ό μ΄μ©ν΄μ charν λ°°μ΄μ λ€λ₯Έ λ°°μ΄μ indexλ‘ μ¬μ©ν΄μ μνλ²³μ μ«μλ‘ μΉν.
π λ²‘ν° μ€λ³΅ μ κ±° λ°©λ²:
λ°λμ sort ν νμ unique ν¨μλ₯Ό μ΄μ©ν΄μ μ κ±°.
// alphabets λ²‘ν° μ€λ³΅ μ κ±°
sort(alphabets.begin(), alphabets.end());
alphabets.erase(unique(alphabets.begin(), alphabets.end()), alphabets.end());
πμ½λ:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int N; // λ¨μ΄ κ°μ
char alpha[128]; // ν΄λΉ μΈλ±μ€μ μνλ²³μ΄ μ΄λ€ μ«μλ‘ λ°λλμ§ μ μ₯ (μΈλ±μ€λ ASCII μ½λ)
int calc(vector<string>& wordArr, vector<char>& alphabets, vector<int>& numbers) {
int s = alphabets.size();
int sum = 0;
for (int i = 0; i < s; i++) {
alpha[alphabets[i]] = numbers[i]; // μνλ²³μ μ«μλ‘ μΉν
}
for (string word : wordArr) {
int now = 0;
for (char x : word) {
now = now * 10 + alpha[x];
}
sum += now;
}
return sum;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
vector<string> wordArr(N);
vector<char> alphabets; // λ¨μ΄μ μ΄λ€ μνλ²³λ€μ΄ μλμ§
for (int i = 0; i < N; i++) {
cin >> wordArr[i];
for (char letter : wordArr[i]) {
alphabets.push_back(letter);
}
}
// alphabets λ²‘ν° μ€λ³΅ μ κ±°
sort(alphabets.begin(), alphabets.end());
alphabets.erase(unique(alphabets.begin(), alphabets.end()), alphabets.end());
int s = alphabets.size();
vector<int> numbers;
for (int i = 9; i > 9 - s; i--) { // μ΅λκ°μ ꡬνλ λ¬Έμ λκΉ 9λΆν° μνλ²³ κ°μλ§νΌ μ«μ κ³ λ₯΄κΈ°
numbers.push_back(i);
}
// μμ΄ μ΅λκ° κ΅¬νκΈ°
int ans = 0;
do {
int tmp = calc(wordArr, alphabets, numbers);
if (ans < tmp) {
ans = tmp;
}
} while (prev_permutation(numbers.begin(), numbers.end()));
cout << ans << '\n';
return 0;
}