티스토리 뷰
https://www.acmicpc.net/problem/30037
const [[N], ...sentence] = require('fs')
.readFileSync('./dev/stdin')
.toString()
.trim()
.split('\n')
.map((v) => v.trim().split(' '));
const answer = [];
sentence.forEach((s) => {
const comp = [];
const comp2 = [];
comp.push(s[0]);
//이 규칙을 여러 번 연달아 적용할 수 있는 경우 앞에서부터 적용한다.
for (let i = 1; i < s.length; i++) {
if (
//단어 'of' 뒤에 문장 부호가 붙어 있는 경우 이 규칙을 적용할 수 없다
comp[comp.length - 1] == 'of'
//'of' 직후에 단어 'Korea'가 등장할 경우
&& (
s[i] =='Korea'
||s[i] =='Korea?'
||s[i] =='Korea.'
||s[i] =='Korea,'
||s[i] =='Korea!'
)
//단어 'of' 앞에 단어가 없거나, 단어 'of' 직전에 나오는 단어에 문장 부호가 붙어 있는 경우 이 규칙을 적용할 수 없다.
&& comp.length >= 2
&& !comp[comp.length - 2].endsWith(',')
&& !comp[comp.length - 2].endsWith('?')
&& !comp[comp.length - 2].endsWith('!')
&& !comp[comp.length - 2].endsWith('.') //??? 이거는 필요 없을 듯..
) {
const of = comp.pop();
let word = comp.pop();
const korea = s[i];
// 단어 'of' 직전에 나오는 단어의 첫 글자가 소문자일 경우 대문자로 변환한다.
if (word.charCodeAt(0) >= 97 && word.charCodeAt(0) <= 122) {
word = word[0].toUpperCase() + word.slice(1);
}
//단어 'Korea' 뒤에 문장 부호가 붙어있는 경우, 단어 'Korea' 뒤의 문장 부호를 단어 'of' 직전에 나오는 단어 뒤에 붙인다.
if (
korea[korea.length - 1] == '!' ||
korea[korea.length - 1] == ',' ||
korea[korea.length - 1] == '?' ||
korea[korea.length - 1] == '.'
) {
word += korea[korea.length - 1];
}
const newWord = 'K-' + word;
comp.push(newWord);
} else {
comp.push(s[i]);
}
}
comp2.push(comp.pop());
for (let j = comp.length - 1; j >= 0; j--) {
if (comp[j] == 'Korea') {
let word = comp2.shift();
if (word.charCodeAt(0) >= 97 && word.charCodeAt(0) <= 122) {
word = word[0].toUpperCase() + word.slice(1);
}
comp2.unshift('K-' + word);
} else {
comp2.unshift(comp[j]);
}
}
answer.push(comp2.join(' '));
});
console.log(answer.join('\n'));
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
230921, 22, 23 (0) | 2023.09.24 |
---|---|
Node.js) 백준 28281번: 선물 (0) | 2023.09.19 |
Node.js) 백준 30035번: Tier and Rank (0) | 2023.09.17 |
Node.js) 백준 30036번: INK (0) | 2023.09.17 |
Node.js) 백준 30034번: Slice String (0) | 2023.09.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BFS
- 투포인터 연습
- create databases;
- 최소공통조상
- 개발자면접
- create db
- 로드나인
- MySQL
- 그래프
- 다이나믹프로그래밍
- 롱베케이션
- 면접질문
- 다이나밍프로그래밍
- 면접비
- 동적프로그래밍
- DB 생성
- 서버개발
- 서버점검
- MOD
- 투포인터
- node.js
- 은둔청년체험
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함