티스토리 뷰

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

 

28432번: 끝말잇기

첫 줄에 끝말잇기 기록의 길이 $N$ 이 주어집니다. $(1 \le N \le 100)$ 둘째 줄부터 다음 $N$개의 줄에는 끝말잇기의 기록 $S_1, \cdots, S_N$이 한 줄에 하나씩 주어집니다. 여기서, 하나의 $S_i$는 “?” 로

www.acmicpc.net

 

const input = require('fs')
	.readFileSync('./dev/stdin')
	.toString()
	.trim()
	.split('\n')
	.map((v) => v.trim());
const N = +input.shift();
const words = [];
let Q;
for (let i = 0; i < N; i++) {
	const word = input.shift();
	words.push(word);
	if (word == '?') Q = i;
}
let front = '';
if (Q > 0) {
	front = words[Q - 1].split('').pop();
}

let rear = '';
if (Q + 1 < words.length) {
	rear = words[Q + 1].split('').shift();
}

const M = +input.shift();

for (let i = 0; i < M; i++) {
	const possible = input.shift();
	if (words.includes(possible)) continue;
	if (possible.startsWith(front) && possible.endsWith(rear)) {
		console.log(possible);
		break;
	}
}
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
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
글 보관함