알고리즘 풀이/백준

★ [백준][Java] 14425번 문자열 집합 (문자열, HashMap)

배게 2021. 9. 23. 18:18
728x90

시간 단축

List로 완전탐색 -> HashMap으로 Key값을 이용한 탐색

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
 
 
public class Main {
        
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
    
    public static void main(String[] args) throws IOException{
        String[] str = br.readLine().split(" ");
        int N = stoi(str[0]);
        int M = stoi(str[1]);
        
        Map<String, Integer> hm = new HashMap<>();
        
        for(int i=0; i<N; i++)
            hm.put(br.readLine(), 1); 
        
        int res = 0;
        for(int i=0; i<M; i++) {
            String input = br.readLine();
            res = (hm.getOrDefault(input, 0)==1)? res+1 : res;
        }
        
        System.out.println(res);
        
//        bw.write("");
//        bw.flush();
//        bw.close();
        
            
        
    }
    
    private static int stoi(String input) {
        return Integer.parseInt(input);
    }
    
}
cs