HyeLog
๋ฐฑ์ค_1339_๋จ์ด์ํ ๋ณธ๋ฌธ
๐๋ฌธ์ : 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;
}
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค_14889_์คํํธ์ ๋งํฌ(๋ธ๋ฃจํธํฌ์ค-์์ด) (0) | 2022.05.10 |
---|---|
๋ฐฑ์ค_14888_์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ (๋ธ๋ฃจํธํฌ์ค-์์ด) (0) | 2022.05.10 |
๋ฐฑ์ค_2529_๋ถ๋ฑํธ (0) | 2022.05.10 |
๋ฐฑ์ค_2580_์ค๋์ฟ (0) | 2022.05.04 |
๋ฐฑ์ค_9663_N-Queen (๋ฐฑํธ๋ํน) (0) | 2022.05.04 |