티스토리 뷰
1764번: 듣보잡
첫째 줄에 듣도 못한 사람의 수 N, 보도 못한 사람의 수 M이 주어진다. 이어서 둘째 줄부터 N개의 줄에 걸쳐 듣도 못한 사람의 이름과, N+2째 줄부터 보도 못한 사람의 이름이 순서대로 주어진다.
www.acmicpc.net
풀이
1) N 개 만큼 맵에 넣음
2) M 개 만큼 맵에 넣는데 이미 넣었던거라면 리스트에 넣음
3) 리스트를 정렬함
4) BufferedWriter에 사이즈와 리스트를 넣고 출력
주의사항
1) x
package com.baekJoon;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class BJ_S4_1764_듣보잡 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer tokens;
static int N,M;
public static void main(String[] args) throws NumberFormatException, IOException {
input = new BufferedReader(new StringReader(src));
tokens = new StringTokenizer(input.readLine());
N = Integer.parseInt(tokens.nextToken());
M = Integer.parseInt(tokens.nextToken());
Map<String, Integer> map = new HashMap<>();
List<String> list = new ArrayList<>();
for(int n=0; n<N; n++) {
String temp = input.readLine();
map.put(temp, 1);
}
for(int m=0; m<M; m++) {
String temp = input.readLine();
if(map.get(temp) != null) {
list.add(temp);
}
}
Collections.sort(list);
output.append(list.size()+"\n");
for(int i=0; i<list.size(); i++) {
output.append(list.get(i)+"\n");
}
output.close();
}
static String src =
"3 4\r\n" +
"ohhenrie\r\n" +
"charlie\r\n" +
"baesangwook\r\n" +
"obama\r\n" +
"baesangwook\r\n" +
"ohhenrie\r\n" +
"clinton";
}
후기
출제자의 의도는 이분 탐색인것 같은데 맵을 이용하면 정말 쉬운 문제였다.
println 으로 출력할 시 BufferedWriter 보다 80ms나 느리다.
'Algorithm' 카테고리의 다른 글
[백준] G4 2638 치즈 (java) (0) | 2021.01.05 |
---|---|
[백준] G5 16954 움직이는 미로 탈출 (java) (0) | 2021.01.05 |
[백준] S2 2491 수열 (java) (0) | 2021.01.04 |
[백준] S3 2559 수열 (java) (0) | 2021.01.04 |
[백준] G5 1600 말이 되고픈 원숭이 (java) (1) | 2021.01.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- S2
- 코딩새내기
- DFS
- java
- 문자열
- PriorityQueue
- react
- 다익스트라
- 백트래킹
- g4
- SWEA
- 리액트
- 시뮬레이션
- Spring Boot
- S3
- G5
- 객체지향
- map
- BFS
- laugh4mile
- 현꾸라지
- 구현
- 알고리즘
- Spring
- react native
- 우선순위큐
- 리액트 네이티브
- 백준
- 자바
- 그리디
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함