단어에 0~9의 값을 중복되지 않게 부여해서 가장 큰 값을 만들어내는 문제입니다.
1. 각 알파벳이 몇의 자리에 값들을 가지고 있는지 확인하여 합산합니다.
2. 합산한 값을 내림차순으로 정렬합니다.
3. 내림차순된 배열에 차례대로 9부터 1까지 곱해줍니다. (v[0] * 9, v[1] * 8 ... v[8] * 1)
4. 곱한값들을 더해서 출력해줍니다.
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
int main (int argc, char *argv[])
{
int n;
int score = 9;
int index;
int result = 0;
vector<int> alp(26);
vector<string> v;
string str;
string text[10];
cin >> n;
for(int i=0; i<n; ++i){
cin >> str;
v.push_back(str);
}
for(int i=0; i<n; ++i){
for(int j=0; j<v[i].length(); ++j){
index = v[i][j] - 'A';
alp[index] += pow(10, v[i].length() - j - 1);
}
}
sort(alp.begin(), alp.end(), greater<int>());
for(int i=0; i<10; ++i){
result += alp[i] * (9 - i);
}
cout << result << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 1926번 그림 (0) | 2021.08.18 |
---|---|
[C++] 백준 1715번 카드 정렬하기 (0) | 2021.08.09 |
[C++] 백준 1744번 수 묶기 (0) | 2021.08.04 |
[C++] 백준 4358번 생태학 (0) | 2021.08.04 |
[C++] 프로그래머스 - 압축 (0) | 2021.05.26 |