반응형
https://www.acmicpc.net/problem/10816
[풀이과정]
HashMap 사용.
이분탐색 방식은 이분탐색을 두 번 돌려서 한 번은 left 값을, 한 번은 right 값을 구해서 개수를 구하기.
[코드]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/*
카드 N개
정수 M개가 주어졌을 때 이 수가 적혀있는 카드를 몇 개 가지고 있는지 구하기
*/
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
static int N, M;
static int[] nums;
static HashMap<Integer, Integer> cards;
public static void main(String[] args) throws IOException{
init();
/*
[출력]
M개의 수에 대해 몇 개 가지고 있는지 공백으로 구분해 출력
*/
for(int m = 0; m < M; m++){
if(cards.containsKey(nums[m])) {
sb.append(cards.get(nums[m]));
}else{
sb.append(0);
}
if(m == M-1){
break;
}
sb.append(" ");
}
System.out.println(sb);
}
static void init() throws IOException {
/*
[입력]
1. 카드 개수 N (1<=N<=500,000)
2~N. 카드에 적힌 정수들
3. M(1<=M<=500,000)
4. 가지고 있는지 구해야 할 M개의 정수 (-10,000,000~10,000,000)
*/
N = Integer.parseInt(br.readLine());
cards = new HashMap<>();
st = new StringTokenizer(br.readLine());
for(int n = 0; n < N; n++){
int key = Integer.parseInt(st.nextToken());
//있으면
if(cards.containsKey(key)){
cards.replace(key, cards.get(key)+1);
}
else{
cards.put(key, 1);
}
}
M = Integer.parseInt(br.readLine());
nums = new int[M];
st = new StringTokenizer(br.readLine());
for(int m = 0; m < M; m++){
nums[m] = Integer.parseInt(st.nextToken());
}
}
}
반응형
'백준 > 자료구조' 카테고리의 다른 글
[스택] 17299 : 오등큰수 (0) | 2023.04.12 |
---|---|
[스택] 17413 : 단어 뒤집기 2 (0) | 2023.04.11 |
[스택] 17298 : 오큰수 (0) | 2023.04.11 |
[백준] 스택 : 9093 (0) | 2023.04.06 |
[백준] 큐 - 10845 (0) | 2023.04.06 |