Skip to content

Commit 03d34ce

Browse files
committed
[Silver I] Title: 민겸 수, Time: 0 ms, Memory: 2028 KB -BaekjoonHub
1 parent 9ed171c commit 03d34ce

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [Silver I] 민겸 수 - 21314
2+
3+
[문제 링크](https://www.acmicpc.net/problem/21314)
4+
5+
### 성능 요약
6+
7+
메모리: 2028 KB, 시간: 0 ms
8+
9+
### 분류
10+
11+
그리디 알고리즘, 구현
12+
13+
### 제출 일자
14+
15+
2025년 5월 24일 17:49:37
16+
17+
### 문제 설명
18+
19+
<p>민겸이는 로마 숫자를 보고 굉장히 흥미롭다고 생각했다. 그래서 민겸이는 새로운 수 체계인 민겸 수를 창조했다.</p>
20+
21+
<p>민겸 숫자는 0 이상의 정수 <em>N</em>에 대해 10<em><sup>N</sup></em> 또는 5 × 10<em><sup>N</sup></em> 꼴의 십진수를 대문자 <code>M</code>과 <code>K</code>로 이루어진 문자열로 표기한다. 10<em><sup>N</sup></em> 꼴의 십진수는 <em>N </em>+ 1개의 <code>M</code>으로, 5 × 10<em><sup>N</sup></em> 꼴의 십진수는 <em>N</em>개의 <code>M</code> 뒤에 1개의 <code>K</code>를 이어붙인 문자열로 나타낸다. 즉, 아래 표처럼 나타낼 수 있다.</p>
22+
23+
<table align="center" border="1" cellpadding="1" cellspacing="1" class="table table-bordered" style="width: 500px;">
24+
<tbody>
25+
<tr>
26+
<td style="text-align: center;">변환 전</td>
27+
<td style="text-align: center;">변환 후</td>
28+
</tr>
29+
<tr>
30+
<td style="text-align: center;"><code>1</code></td>
31+
<td style="text-align: center;"><code>M</code></td>
32+
</tr>
33+
<tr>
34+
<td style="text-align: center;"><code>5</code></td>
35+
<td style="text-align: center;"><code>K</code></td>
36+
</tr>
37+
<tr>
38+
<td style="text-align: center;"><code>10</code></td>
39+
<td style="text-align: center;"><code>MM</code></td>
40+
</tr>
41+
<tr>
42+
<td style="text-align: center;"><code>50</code></td>
43+
<td style="text-align: center;"><code>MK</code></td>
44+
</tr>
45+
<tr>
46+
<td style="text-align: center;"><code>100</code></td>
47+
<td style="text-align: center;"><code>MMM</code></td>
48+
</tr>
49+
<tr>
50+
<td style="text-align: center;"><code>500</code></td>
51+
<td style="text-align: center;"><code>MMK</code></td>
52+
</tr>
53+
<tr>
54+
<td style="text-align: center;"><code>1000</code></td>
55+
<td style="text-align: center;"><code>MMMM</code></td>
56+
</tr>
57+
<tr>
58+
<td style="text-align: center;"><code>5000</code></td>
59+
<td style="text-align: center;"><code>MMMK</code></td>
60+
</tr>
61+
<tr>
62+
<td style="text-align: center;"><code>...</code></td>
63+
<td style="text-align: center;"><code>...</code></td>
64+
</tr>
65+
</tbody>
66+
</table>
67+
68+
<p>민겸 수는 한 개 이상의 민겸 숫자를 이어붙여 만든다. 예를 들어, 민겸 수 <code>MKKMMK</code>는 <code>MK</code>, <code>K</code>, <code>MMK</code>의 세 민겸 숫자를 이어붙여 만들 수 있다.</p>
69+
70+
<p>민겸 수를 십진수로 변환할 때는, 1개 이상의 민겸 숫자로 문자열을 분리한 뒤, 각각의 민겸 숫자를 십진수로 변환해서 순서대로 이어붙이면 된다. 민겸 숫자를 십진수로 변환하는 것은 십진수를 민겸 숫자로 변환하는 과정을 거꾸로 하면 된다. 예를 들어, 민겸 수 <code>MKKMMK</code>는 아래 그림과 같이 여러 가지 십진수로 변환할 수 있다.</p>
71+
72+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/3a65029c-5253-4600-8d93-908e4f368161/-/preview/"></p>
73+
74+
<p>민겸이는 위와 같이 하나의 민겸 수가 다양한 십진수로 변환될 수 있다는 사실을 알았다. 문득 민겸이는 변환될 수 있는 십진수 중 가장 큰 값과 가장 작은 값이 궁금해졌다. 민겸이를 위해 하나의 민겸 수가 십진수로 변환되었을 때 가질 수 있는 최댓값과 최솟값을 구해주자.</p>
75+
76+
### 입력
77+
78+
<p>민겸 수 하나가 주어진다. 민겸 수는 대문자 <code>M</code>과 <code>K</code>로만 이루어진 문자열이며, 길이는 3,000을 넘지 않는다.</p>
79+
80+
### 출력
81+
82+
<p>주어진 민겸 수가 십진수로 변환되었을 때 가질 수 있는 값 중 가장 큰 값과 가장 작은 값을 두 줄로 나누어 출력한다.</p>
83+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <stack>
4+
5+
using namespace std;
6+
7+
vector<int> answer_v;
8+
vector<string> max_v;
9+
vector<int> min_v;
10+
11+
string input;
12+
13+
string find_max() {
14+
stack<char> st;
15+
string temp;
16+
for (const char i : input) {
17+
if (st.empty()) {
18+
if (i == 'K') {
19+
temp.append(to_string(5));
20+
} else if (i == 'M') {
21+
st.push(i);
22+
}
23+
} else { // i == 'M'
24+
if (st.top() == 'M' && i == 'K') {
25+
temp.append(to_string(5));
26+
for (int j = 0; j < st.size(); j++) {
27+
temp.append(to_string(0));
28+
}
29+
30+
// Stack 비우기
31+
while (!st.empty()) {
32+
st.pop();
33+
}
34+
} else {
35+
st.push(i);
36+
}
37+
}
38+
}
39+
40+
if (!st.empty()) {
41+
for (int i = 0; i < st.size(); i++) {
42+
temp.append(to_string(1));
43+
}
44+
}
45+
46+
return temp;
47+
}
48+
49+
string find_min() {
50+
vector<char> M;
51+
string temp;
52+
for (const char i : input) {
53+
// K가 있을 때까지 Cut
54+
if (i == 'K') {
55+
if (!M.empty()) {
56+
temp.append(to_string(1));
57+
for (int j = 0; j < M.size() - 1; j++) {
58+
temp.append(to_string(0));
59+
}
60+
61+
temp.append(to_string(5));
62+
M.clear();
63+
} else if (M.empty()) {
64+
temp.append(to_string(5));
65+
}
66+
} else if (i == 'M') {
67+
M.push_back('M');
68+
}
69+
}
70+
71+
// K가 없고 계속 M이면, 이렇게 처리
72+
if (!M.empty()) {
73+
temp.append(to_string(1));
74+
for (int j = 0; j < M.size() - 1; j++) {
75+
temp.append(to_string(0));
76+
}
77+
}
78+
79+
return temp;
80+
}
81+
82+
int main() {
83+
cin >> input;
84+
cout << find_max() << '\n';
85+
cout << find_min();
86+
87+
return 0;
88+
}

0 commit comments

Comments
 (0)