[백준] 1339번: 단어 수학

짜장범벅·2022년 8월 6일
0

백준

목록 보기
20/26

1 문제

단어의 알파벳을 임의의 숫자로 치환했을 때, 단어의 총 합이 최대가 되는 값을 찾아라

2 Idea

각 알파벳 별 합을 구한 다음, 가장 큰 숫자부터 9, 8, ..., 1 부여

3 Code

//link: https://www.acmicpc.net/problem/1339

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

int FindMaxSum(const std::vector<std::string>& v, const int N){
    int sum = 0;

    std::vector<int> charSum(26, 0);
    
    for (int i=0; i<v.size(); ++i){
        for (int j=v[i].size()-1, n=1; j>=0; --j, n*=10){
            charSum[v[i][j]-'A'] += n;
        }
    }

    std::sort(charSum.begin(), charSum.end(), std::greater<>());

    for (int i=0; i<9; ++i){
        sum += charSum[i]*(9-i);
    }

    return sum;
}

int main(){
    int N = 0;
    std::cin >> N;

    std::vector<std::string> v;
    for (int i=0; i<N; ++i){
        std::string temp;
        std::cin >> temp;

        v.push_back(temp);
    }

    std::cout << FindMaxSum(v, N) << std::endl;
    
    return 0;
}

4 Reference

https://excited-hyun.tistory.com/145

profile
큰일날 사람

0개의 댓글