https://www.acmicpc.net/problem/1339
1339번: 단어 수학
첫째 줄에 단어의 개수 N(1 ≤ N ≤ 10)이 주어진다. 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 대문자로만 이루어져있다. 모든 단어에 포함되어 있는 알파벳은 최대
www.acmicpc.net
서로 다른 알파벳은 10개
0 - 9까지 숫자 가능
경우의 수 : 10!
서로 다른 알파벳이 m개라면
최댓값을 구하는 것이므로 가장 큰 값 m개 넣으면 된다 (9, 8, 7, 6 ....)
unique 함수, erase 함수
unique 함수 unique(v.begin(), v.end())
vector 배열에서 중복되지 않는 원소들을 앞에서부터 채워나가는 함수
erase 함수를 이용하여 필요한 값만 남기기
v.erase(unique(v.begin(), v.end()), v.end())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
char alpha[256];
int calc(vector<string>& a, vector<char>& letters, vector<int>& d)
{
int m = letters.size();
for (int i = 0; i < m; i++)
{
alpha[letters[i]] = d[i];
}
int sum = 0;
for (string s : a)
{
int now = 0;
for (char x : s)
{
now = now * 10 + alpha[x];
}
sum += now;
}
return sum;
}
int main()
{
int n;
cin >> n;
vector<string> a(n);
vector<char> letters;
for (int i = 0; i < n; i++)
{
cin >> a[i];
for (char x : a[i])
letters.push_back(x);
}
sort(letters.begin(), letters.end()); // 소팅을 해야지 시간이 줄어든다
letters.erase(unique(letters.begin(), letters.end()), letters.end());
int m = letters.size();
vector<int> d;
for (int i = 0; i < m; i++)
d.push_back(9 - i);
int ans = 0;
int sum;
do {
sum =calc(a, letters, d);
if (sum > ans)
ans = sum;
} while (prev_permutation(d.begin(), d.end()));
cout << ans << '\n';
return 0;
}
|
cs |
alpha[ letters[i] ] = d[i];
for-each문
'브루트 포스 > 브루트포스(순열)' 카테고리의 다른 글
스타트와 링크 (순열) (0) | 2023.03.20 |
---|---|
연산자 끼워넣기 (0) | 2023.03.20 |
부등호 (순열 풀이) (0) | 2023.03.18 |
로또 (같은 문자를 포함한 순열) (0) | 2023.02.23 |
외판원 순회 2 (0) | 2023.02.20 |