Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 13164
- 플로이드-워셜
- BFS
- 누적 합
- 해시 테이블
- 애드 혹
- 이분 탐색
- Python
- 브루트포스
- 구현
- 투 포인터
- 그리디
- 싸피
- 수학
- 에라토스테네스의 체
- 모던 JavaScript 튜토리얼
- 2357
- JavaScript
- 슬라이딩 윈도우
- 정렬
- SSAFY
- DP
- 맵
- 문자열
- DFS
- 그래프
- 세그먼트 트리
- boj
- 정수론
- 트리
Archives
- Today
- Total
흙금이네 블로그
[BOJ] 14713 - 앵무새 (Python, JavaScript) 본문
아이디어
맵을 이용해 모든 단어들이 순서에 맞게 적혀 있는지 확인한다.
풀이 #1 (Python)
import sys
input = sys.stdin.readline
def solution():
N = int(input())
sentences = [input().split() for _ in range(N)]
idx_dict = dict()
for i in range(N):
idx_dict[sentences[i].pop()] = i
L = input().split()
while L:
word = L.pop()
idx = idx_dict.get(word, -1)
idx_dict[word] = -1
if idx >= 0:
if sentences[idx]:
idx_dict[sentences[idx].pop()] = idx
else:
print('Impossible')
return
print('Possible')
solution()
풀이 #2 (JavaScript)
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split('\n');
function solution() {
const N = Number(input[0]);
let sentences = [];
for (let i=1; i<=N; i++) {
sentences.push(input[i].split(' '));
}
let idxMap = new Map();
for (let i=0; i<N; i++) {
idxMap.set(sentences[i].pop(), i+1);
}
let L = input[N+1].split(' ');
while (L.length) {
const word = L.pop();
const idx = idxMap.get(word);
idxMap.set(word, 0);
if (idx) {
if (sentences[idx-1]) idxMap.set(sentences[idx-1].pop(), idx);
}
else {
console.log('Impossible');
return
}
}
console.log('Possible');
}
solution();
'알고리즘' 카테고리의 다른 글
[BOJ] 6597 - 트리 복구 (Python, JavaScript) (0) | 2023.05.24 |
---|---|
[BOJ] 7432 - 디스크 트리 (Python, JavaScript) (0) | 2023.05.23 |
[BOJ] 2257 - 화학식량 (Python, JavaScript) (1) | 2023.05.21 |
[BOJ] 1411 - 비슷한 단어 (Python, JavaScript) (0) | 2023.05.20 |
[BOJ] 2136 - 개미 (Python, JavaScript) (0) | 2023.05.19 |
Comments