Skip to content

Commit 7c12aa3

Browse files
committed
[Gold III] Title: K-Words Problem, Time: 240 ms, Memory: 81392 KB -BaekjoonHub
1 parent 18f4847 commit 7c12aa3

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import Foundation
2+
3+
func isPunc(_ s: String) -> Bool {
4+
return s.count == 1 && "!?,.".contains(s)
5+
}
6+
7+
func capitalizeFirst(_ s: String) -> String {
8+
guard let first = s.first else { return s }
9+
let firstUp = String(first).uppercased()
10+
let rest = String(s.dropFirst())
11+
return firstUp + rest
12+
}
13+
14+
func reconstruct(_ tokens: [String]) -> String {
15+
var out = ""
16+
for i in 0..<tokens.count {
17+
out += tokens[i]
18+
if i + 1 < tokens.count {
19+
if !isPunc(tokens[i+1]) {
20+
out += " "
21+
}
22+
}
23+
}
24+
return out
25+
}
26+
27+
func applyOfKoreaRule(_ tokens: inout [String]) {
28+
var i = 0
29+
while i + 2 < tokens.count {
30+
let w = tokens[i]
31+
let of = tokens[i+1]
32+
let kr = tokens[i+2]
33+
if of == "of" && kr == "Korea" && !isPunc(w) {
34+
let newWord = "K-" + capitalizeFirst(w)
35+
tokens[i] = newWord
36+
tokens.remove(at: i+2)
37+
tokens.remove(at: i+1)
38+
} else {
39+
i += 1
40+
}
41+
}
42+
}
43+
44+
func applyKoreaRule(_ tokens: inout [String]) {
45+
var i = tokens.count - 2
46+
while i >= 0 {
47+
if tokens[i] == "Korea" {
48+
let next = tokens[i+1]
49+
if !isPunc(next) {
50+
let newWord = "K-" + capitalizeFirst(next)
51+
tokens[i+1] = newWord
52+
tokens.remove(at: i)
53+
}
54+
}
55+
i -= 1
56+
}
57+
}
58+
59+
func solveLine(_ line: String) -> String {
60+
var mod = ""
61+
for c in line {
62+
if !c.isLetter && !c.isNumber && c != "_" && c != "-" && !c.isWhitespace {
63+
mod.append(" ")
64+
mod.append(c)
65+
mod.append(" ")
66+
} else {
67+
mod.append(c)
68+
}
69+
}
70+
let raw = mod.trimmingCharacters(in: .whitespaces)
71+
let parts = raw.split { $0.isWhitespace }.map { String($0) }
72+
var tokens = parts
73+
74+
applyOfKoreaRule(&tokens)
75+
applyKoreaRule(&tokens)
76+
77+
return reconstruct(tokens)
78+
}
79+
80+
if let nLine = readLine(), let N = Int(nLine) {
81+
for _ in 0..<N {
82+
if let line = readLine() {
83+
print(solveLine(line))
84+
}
85+
}
86+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [Gold III] K-Words Problem - 30037
2+
3+
[문제 링크](https://www.acmicpc.net/problem/30037)
4+
5+
### 성능 요약
6+
7+
메모리: 81392 KB, 시간: 240 ms
8+
9+
### 분류
10+
11+
자료 구조, 구현, 스택, 문자열
12+
13+
### 제출 일자
14+
15+
2025년 11월 26일 19:13:33
16+
17+
### 문제 설명
18+
19+
<p>아티초크는 교양과목 '한국 문화로 보는 프로그래밍 언어론'을 수강 중이다.</p>
20+
21+
<p>아티초크는 지난주에 과제로 '아희가 한국 문화에 미친 영향'을 주제로 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mn class="mjx-n"><mjx-c class="mjx-c35"></mjx-c></mjx-mn><mjx-mstyle><mjx-mspace style="width: 0.167em;"></mjx-mspace></mjx-mstyle><mjx-mn class="mjx-n"><mjx-c class="mjx-c30"></mjx-c><mjx-c class="mjx-c30"></mjx-c><mjx-c class="mjx-c30"></mjx-c></mjx-mn></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn>5</mn><mstyle scriptlevel="0"><mspace width="0.167em"></mspace></mstyle><mn>000</mn></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$5\,000$</span></mjx-container>단어 분량의 보고서를 작성했는데, 이번 주에는 이 보고서를 짧게 요약하는 과제가 나왔다.</p>
22+
23+
<p>이번 학기에 전공과목만 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mn class="mjx-n"><mjx-c class="mjx-c37"></mjx-c></mjx-mn></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn>7</mn></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$7$</span></mjx-container>과목을 수강하고 있었던 아티초크는 보고서 요약을 직접 할 여유가 없었다.</p>
24+
25+
<p>따라서 아래의 규칙에 따라 보고서를 요약해주는 프로그램을 만들기로 했다. 규칙은 다음과 같다.</p>
26+
27+
<ol>
28+
<li>보고서에 단어 'Korea'가 등장할 경우, 다음에 나오는 단어와 합쳐 'K-단어'로 축약한다. (ex. Korea language → K-Language)
29+
<ul>
30+
<li>단어 'Korea'는 대문자와 소문자를 구분한다.</li>
31+
<li>단어 'Korea' 다음에 나오는 단어의 첫 글자가 소문자일 경우 대문자로 변환한다.</li>
32+
<li>이 규칙을 여러 번 연달아 적용할 수 있는 경우 뒤에서부터 적용한다.</li>
33+
<li>단어 'Korea' 뒤에 문장 부호가 붙어 있는 경우 이 규칙을 적용할 수 없다.</li>
34+
</ul>
35+
</li>
36+
<li>보고서에 단어 'of' 직후에 단어 'Korea'가 등장할 경우, 단어 'of' 직전에 나오는 단어와 합쳐 K-단어로 축약한다. (ex. Language of Korea → K-Language)
37+
<ul>
38+
<li>단어 'of'와 'Korea'는 대문자와 소문자를 구분한다.</li>
39+
<li>단어 'of' 직전에 나오는 단어의 첫 글자가 소문자일 경우 대문자로 변환한다.</li>
40+
<li>이 규칙을 여러 번 연달아 적용할 수 있는 경우 앞에서부터 적용한다.</li>
41+
<li>단어 'of' 뒤에 문장 부호가 붙어 있는 경우 이 규칙을 적용할 수 없다.</li>
42+
<li>단어 'of' 앞에 단어가 없거나, 단어 'of' 직전에 나오는 단어에 문장 부호가 붙어 있는 경우 이 규칙을 적용할 수 없다.</li>
43+
<li>단어 'Korea' 뒤에 문장 부호가 붙어있는 경우, 단어 'Korea' 뒤의 문장 부호를 단어 'of' 직전에 나오는 단어 뒤에 붙인다. </li>
44+
</ul>
45+
</li>
46+
<li>1번 규칙과 2번 규칙을 한 문장에 동시에 적용할 수 있는 경우, 2번 규칙을 먼저 적용한다.</li>
47+
</ol>
48+
49+
<p>아티초크는 전공 과제가 너무 밀려, 보고서 요약 프로그램을 작성할 여유조차 없었다. 그런 아티초크를 대신하여 구현해 보자.</p>
50+
51+
### 입력
52+
53+
<p>첫 번째 줄에 문장의 개수 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi><mjx-mo class="mjx-n"><mjx-c class="mjx-c28"></mjx-c></mjx-mo><mjx-mn class="mjx-n"><mjx-c class="mjx-c31"></mjx-c></mjx-mn><mjx-mo class="mjx-n" space="4"><mjx-c class="mjx-c2264"></mjx-c></mjx-mo><mjx-mi class="mjx-i" space="4"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi><mjx-mo class="mjx-n" space="4"><mjx-c class="mjx-c2264"></mjx-c></mjx-mo><mjx-mn class="mjx-n" space="4"><mjx-c class="mjx-c35"></mjx-c></mjx-mn><mjx-mstyle><mjx-mspace style="width: 0.167em;"></mjx-mspace></mjx-mstyle><mjx-mn class="mjx-n"><mjx-c class="mjx-c30"></mjx-c><mjx-c class="mjx-c30"></mjx-c><mjx-c class="mjx-c30"></mjx-c></mjx-mn><mjx-mo class="mjx-n"><mjx-c class="mjx-c29"></mjx-c></mjx-mo></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi><mo stretchy="false">(</mo><mn>1</mn><mo>≤</mo><mi>N</mi><mo>≤</mo><mn>5</mn><mstyle scriptlevel="0"><mspace width="0.167em"></mspace></mstyle><mn>000</mn><mo stretchy="false">)</mo></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N (1 \le N \le 5\,000)$</span></mjx-container> 이 주어진다.</p>
54+
55+
<p>다음 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N$</span></mjx-container>개의 줄에 알파벳 대소문자와 문장부호 느낌표(!), 물음표(?), 쉼표(,), 마침표(.)로 구성된 문장이 주어진다.</p>
56+
57+
<p>각 문장들은 하나 이상의 단어로 이루어져 있으며, 각 단어마다 하나 이상의 알파벳이 존재함이 보장된다.</p>
58+
59+
<p>한 단어의 길이는 최대 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mn class="mjx-n"><mjx-c class="mjx-c35"></mjx-c><mjx-c class="mjx-c30"></mjx-c></mjx-mn></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn>50</mn></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$50$</span></mjx-container>자, 입력으로 들어오는 단어들의 수의 합은 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mn class="mjx-n"><mjx-c class="mjx-c35"></mjx-c></mjx-mn><mjx-mstyle><mjx-mspace style="width: 0.167em;"></mjx-mspace></mjx-mstyle><mjx-mn class="mjx-n"><mjx-c class="mjx-c30"></mjx-c><mjx-c class="mjx-c30"></mjx-c><mjx-c class="mjx-c30"></mjx-c></mjx-mn></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn>5</mn><mstyle scriptlevel="0"><mspace width="0.167em"></mspace></mstyle><mn>000</mn></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$5\,000$</span></mjx-container>을 넘지 않는다.</p>
60+
61+
<p>단어들 사이는 하나의 공백(띄어쓰기)로 구분하며, 문장들 사이는 줄바꿈 문자로 구분한다.</p>
62+
63+
<p>문장부호는 단어의 끝에서만 등장할 수 있으며, 단어 하나당 문장부호는 최대 <mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"><mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mn class="mjx-n"><mjx-c class="mjx-c31"></mjx-c></mjx-mn></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mn>1</mn></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$1$</span></mjx-container>개까지 들어갈 수 있다.</p>
64+
65+
<p>특히 마침표의 경우 문장의 마지막 단어에 반드시 등장하며, 이외의 위치에서는 등장하지 않는다.</p>
66+
67+
### 출력
68+
69+
<p><mjx-container class="MathJax" jax="CHTML" style="font-size: 109%; position: relative;"> <mjx-math class="MJX-TEX" aria-hidden="true"><mjx-mi class="mjx-i"><mjx-c class="mjx-c1D441 TEX-I"></mjx-c></mjx-mi></mjx-math><mjx-assistive-mml unselectable="on" display="inline"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>N</mi></math></mjx-assistive-mml><span aria-hidden="true" class="no-mathjax mjx-copytext">$N$</span></mjx-container>개의 줄에 문제에서 제시된 규칙에 맞게 요약된 보고서를 출력한다.</p>
70+

0 commit comments

Comments
 (0)