티스토리 뷰

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

 

1501번: 영어 읽기

첫째 줄에 사전에 있는 단어들의 개수 N(0 ≤ N ≤ 10,000)이 주어진다. 다음 N개의 줄에는 각 줄에 하나씩, 영어 사전에 있는 단어들이 주어진다. 각 단어의 길이는 100자를 넘지 않는다. 다음 줄에

www.acmicpc.net

const input = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const dict = new Map();
for (let i = 0; i < N; i++) {
	const word = input.shift().trim().split('');
	const headTail = word.shift() + (word.pop() ?? '');
	const body = word.sort().join('');

	if (dict.has(headTail)) {
		const subdict = dict.get(headTail);
		if (subdict.has(body)) {
			const cnt = subdict.get(body);
			subdict.set(body, cnt + 1);
		} else {
			subdict.set(body, 1);
		}
	} else {
		const subdict = new Map();
		subdict.set(body, 1);
		dict.set(headTail, subdict);
	}
}
const answer = [];
const M = +input.shift();
for (let i = 0; i < M; i++) {
	let tcAnswer = 0;
	const words = input.shift().trim();
	words.split(' ').forEach((word) => {
		word = word.split('');
		const headTail = word.shift() + (word.pop() ?? '');
		const body = word.sort().join('');
		if (dict.has(headTail)) {
			const subdict = dict.get(headTail);
			if (subdict.has(body)) {
				if (tcAnswer == 0) tcAnswer = 1;
				tcAnswer *= subdict.get(body);
			}
		}
	});
	answer.push(tcAnswer);
}
console.log(answer.join('\n'));
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함