Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
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
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-1269번/Java] 대칭 차집합 본문

Algorithm

[백준-1269번/Java] 대칭 차집합

1984 2022. 11. 22. 17:11

https://www.acmicpc.net/problem/1269

 

1269번: 대칭 차집합

첫째 줄에 집합 A의 원소의 개수와 집합 B의 원소의 개수가 빈 칸을 사이에 두고 주어진다. 둘째 줄에는 집합 A의 모든 원소가, 셋째 줄에는 집합 B의 모든 원소가 빈 칸을 사이에 두고 각각 주어

www.acmicpc.net

 

* HashMap을 사용해 교집합을 구했다.

copy_A.entrySet().retainAll(B.entrySet());

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer stk = new StringTokenizer(br.readLine(), " ");

		int N = Integer.parseInt(stk.nextToken());
		int M = Integer.parseInt(stk.nextToken());

		Map<String, Integer> A = new HashMap<String, Integer>();
		Map<String, Integer> B = new HashMap<String, Integer>();

		StringTokenizer a_token = new StringTokenizer(br.readLine(), " ");
		StringTokenizer b_token = new StringTokenizer(br.readLine(), " ");

		for (int i = 0; i < N; i++) {
			A.put(a_token.nextToken(), 1);
		}

		for (int i = 0; i < M; i++) {
			B.put(b_token.nextToken(), 1);
		}

		Map<String, Integer> copy_A = new HashMap<String, Integer>(A);
		
		copy_A.entrySet().retainAll(B.entrySet());

		System.out.println(N + M - copy_A.size() * 2);
	}

}
Comments