티스토리 뷰

https://www.acmicpc.net/problem/30037

 

30037번: K-Words Problem

첫 번째 줄에 문장의 개수 $N (1 \le N \le 5\,000)$ 이 주어진다. 다음 $N$개의 줄에 알파벳 대소문자와 문장부호 느낌표(!), 물음표(?), 쉼표(,), 마침표(.)로 구성된 문장이 주어진다. 각 문장들은 하나

www.acmicpc.net

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
링크
«   2024/09   »
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
글 보관함