๊ธฐ๋ก๋ฐฉ

BOJ_1269 : ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1269 : ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ

Soom_1n 2022. 12. 6. 18:10

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

 

1269๋ฒˆ: ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ

์ฒซ์งธ ์ค„์— ์ง‘ํ•ฉ A์˜ ์›์†Œ์˜ ๊ฐœ์ˆ˜์™€ ์ง‘ํ•ฉ B์˜ ์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ง‘ํ•ฉ A์˜ ๋ชจ๋“  ์›์†Œ๊ฐ€, ์…‹์งธ ์ค„์—๋Š” ์ง‘ํ•ฉ B์˜ ๋ชจ๋“  ์›์†Œ๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ๊ฐ๊ฐ ์ฃผ์–ด

www.acmicpc.net



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

  • ๋‘ ์ง‘ํ•ฉ์„ ์ž…๋ ฅ๋ฐ›๊ณ  ๊ฐ๊ฐ ์ฐจ์ง‘ํ•ฉ์„ ๊ณ„์‚ฐํ•ด์„œ ์›์†Œ ๊ฐœ์ˆ˜์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

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));
        HashSet<String> set_A = new HashSet<>();
        HashSet<String> set_B = new HashSet<>();
        StringTokenizer st = new StringTokenizer(br.readLine());

        st = new StringTokenizer(br.readLine());
        while (st.hasMoreTokens()) {
            set_A.add(st.nextToken());
        }

        st = new StringTokenizer(br.readLine());
        while (st.hasMoreTokens()) {
            set_B.add(st.nextToken());
        }

        HashSet<String> set_retain = new HashSet<>();
        set_retain.addAll(set_A);
        set_retain.retainAll(set_B);

        set_A.removeAll(set_retain);
        set_B.removeAll(set_retain);

        System.out.println(set_A.size()+set_B.size());
    }
}

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

  • ํ•ด์‹œ ์…‹์„ ์ด์šฉํ•ด ๋‘ ์ง‘ํ•ฉ์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
    • set_A, set_B
  • set_retain์— set_A์™€ set_B์˜ ๊ต์ง‘ํ•ฉ์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  • set_A์™€ set_B์— ๊ฐ๊ฐ set_retain๊ณผ ์ฐจ์ง‘ํ•ฉ ์—ฐ์‚ฐํ›„ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ•ฉํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ฐจ์ง‘ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” removeAll()์ด ๊ฒฐ๊ณผ ์…‹์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฒŒ ์•„๋‹Œ ์›๋ณธ์„ ๋ฐ”๊พธ๋Š” ์—ฐ์‚ฐ์ด์–ด์„œ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ํ’€์ดํ•  ์ง€ ๊ณ ๋ฏผ๋˜์—ˆ๋‹ค.

728x90

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

BOJ_1002 : ํ„ฐ๋ ›  (0) 2022.12.07
BOJ_14425 : ๋ฌธ์ž์—ด ์ง‘ํ•ฉ  (0) 2022.12.06
BOJ_9733 : ๊ฟ€๋ฒŒ  (0) 2022.12.06
BOJ_11971 : ์†๋„ ์œ„๋ฐ˜  (0) 2022.12.06
BOJ_24389 : 2์˜ ๋ณด์ˆ˜  (0) 2022.12.05