๊ธฐ๋ก๋ฐฉ

BOJ_14425 : ๋ฌธ์ž์—ด ์ง‘ํ•ฉ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_14425 : ๋ฌธ์ž์—ด ์ง‘ํ•ฉ

Soom_1n 2022. 12. 6. 18:15

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

14425๋ฒˆ: ๋ฌธ์ž์—ด ์ง‘ํ•ฉ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ N๊ณผ M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค.  ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • n๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ง‘ํ•ฉ์œผ๋กœ ์ €์žฅํ•œ๋‹ค.
  • m๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅ๋œ ์ง‘ํ•ฉ์— ํฌํ•จ๋œ ๋ฌธ์ž์—ด์ด ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        HashSet<String> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            set.add(br.readLine());
        }

        int count = 0;
        for (int i = 0; i < m; i++) {
            if (set.contains(br.readLine()))
                count++;
        }
        System.out.println(count);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • n๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด์‹œ ์…‹์— ์ €์žฅํ•œ๋‹ค.
  • m๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ contains๋กœ ํ•ด์‹œ ์…‹์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์นด์šดํŠธํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • HashSet์˜ contains๋ฅผ ์•Œ๊ณ ์žˆ์–ด์„œ ์‰ฝ๊ฒŒ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_15649 : N๊ณผ M (1)  (0) 2022.12.08
BOJ_1002 : ํ„ฐ๋ ›  (0) 2022.12.07
BOJ_1269 : ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ  (0) 2022.12.06
BOJ_9733 : ๊ฟ€๋ฒŒ  (0) 2022.12.06
BOJ_11971 : ์†๋„ ์œ„๋ฐ˜  (0) 2022.12.06