[백준 27386] Class Field Trip (C#, C++) - soo:bak
작성일 :
문제 링크
설명
정렬된 두 문자열이 주어질 때, 알파벳 순서를 유지하며 합친 문자열을 출력하는 문제입니다.
접근법
먼저 두 문자열의 포인터를 앞에서부터 움직이며 작은 문자를 선택합니다.
다음으로 한쪽이 끝나면 남은 문자를 그대로 이어 붙입니다.
마지막으로 완성된 문자열을 출력합니다.
Code
C#
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
using System;
using System.Text;
class Program {
static void Main() {
var a = Console.ReadLine()!;
var b = Console.ReadLine()!;
var i = 0;
var j = 0;
var sb = new StringBuilder();
while (i < a.Length && j < b.Length) {
if (a[i] <= b[j]) {
sb.Append(a[i]);
i++;
} else {
sb.Append(b[j]);
j++;
}
}
while (i < a.Length) {
sb.Append(a[i]);
i++;
}
while (j < b.Length) {
sb.Append(b[j]);
j++;
}
Console.WriteLine(sb.ToString());
}
}
C++
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string a, b; cin >> a >> b;
int i = 0;
int j = 0;
string out;
while (i < (int)a.size() && j < (int)b.size()) {
if (a[i] <= b[j]) {
out.push_back(a[i]);
i++;
} else {
out.push_back(b[j]);
j++;
}
}
while (i < (int)a.size()) {
out.push_back(a[i]);
i++;
}
while (j < (int)b.size()) {
out.push_back(b[j]);
j++;
}
cout << out << "\n";
return 0;
}