1일1알

백준 1339번 단어 수학 C++ 본문

알고리즘

백준 1339번 단어 수학 C++

영춘권의달인 2022. 8. 12. 17:02

출처 : https://www.acmicpc.net/problem/1339

 

알파벳이 앞에 나올수록 가산점을 더 부여해서 알파벳의 개수를 저장하고 저장한 알파벳의 개수가 많은 순서대로 숫자를 할당하였다.

예를들어 AA, BC 가 있다면 A는 10 + 1 = 11, B는 10, C는 1이 되고 A에 9, B에 8, C에 7을 할당한다.

만약 알파벳의 길이가 다르다면 가장 긴 알파벳의 개수를 기준으로 짧은 알파벳 앞에 @를 더해서 건너뛰도록 하였다.

예를들어 ABC, DE가 있다면 DE앞에 @를 붙여 @DE로 만들어서 길이를 맞춰주고 @인 경우는 연산을 하지 않도록 했다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n;
    cin >> n;
    vector<string> strs(n);
    int maxLength = 0;
    for (int i = 0; i < n; i++) {
        cin >> strs[i];
        maxLength = max(maxLength, int(strs[i].length()));
    }
    for (int i = 0; i < n; i++) {
        int currLength = strs[i].length();
        for (int j = 0; j < maxLength - currLength; j++) {
            strs[i] = "@" + strs[i];
        }
    }
    int num = 9;
    int ans = 0;
    map<char, int> mp;
    multimap<int, char, greater<int>> mmp;
    for (int i = 0; i < maxLength; i++) {
        int mul = pow(10, maxLength - i - 1);
        for (int j = 0; j < n; j++) {
            if (strs[j][i] == '@') continue;
            mp[strs[j][i]] += mul;
        }
    }
    for (auto a : mp) {
        mmp.insert({ a.second,a.first });
    }
    for (auto a : mmp) {
        ans += a.first * num;
        num--;
    }
    cout << ans;
}