[ 백준 ] 1339 / 단어 수학

金弘均·2021년 9월 15일
0

Baekjoon Online Judge

목록 보기
106/228
post-thumbnail

# Appreciation

/*
 * Problem :: 1339 / 단어 수학
 *
 * Kind :: Math
 *
 * Insight
 * - 각 알파벳이 가지는 계수(가중치)를 다 더해서 큰 순서대로 정렬한 후,
 *   차례로 9, 8, 7, 6, ... 부여하면 된다
 *   + Ex) AB + CDA
 *     A : 11, B : 1, C : 100, D : 10
 *     C = 9, A = 8, D = 7, B = 6
 */

# Code

//
//  BOJ
//  ver.C++
//
//  Created by GGlifer
//
//  Open Source

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

#define endl '\n'

// Set up : Global Variables
/* None */

// Set up : Functions Declaration
/* None */


int main()
{
    // Set up : I/O
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Set up : Input
    int N; cin >> N;
    string words[N];
    for (int i=0; i<N; i++) {
        cin >> words[i];
    }

    // Process
    map<char,int> coeff; /* 각 알파벳에 따른 계수의 합이 저장됨 */
    for (int i=0; i<N; i++) {
        int offset = 1;
        /* 계수 계산을 쉽게 하기위해 단어를 뒤집어줌 */
        reverse(words[i].begin(), words[i].end());
        for (char c : words[i]) {
            coeff[c] += offset;
            offset *= 10;
        }
    }

    /* Sort by value */
    vector<int> weight;
    for (auto [letter, coeff] : coeff) {
        weight.push_back(coeff);
    } sort(weight.begin(), weight.end(), greater<>());

    int ans = 0;
    /* 값을 최대로 만들기 위해서는 큰 계수를 가진 알파벳에 큰 값이 들어가야 함 */
    for (int i=0; i<weight.size(); i++) {
        ans += weight[i] * (9-i);
    }

    // Control : Output
    cout << ans << endl;
}

// Helper Functions
/* None */
profile
이런 미친 게임을 봤나! - 옥냥이

0개의 댓글