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

 

1269번: 대칭 차집합

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

www.acmicpc.net

문제 요약 : 주어진 집합 A와 B의 대칭 차집합 원소 개수 출력

입력 출력
1 ≤ A, B(원소 개수) ≤ 200000
1 ≤ A[N], B[N] ≤ 100000000
A, B의 대칭차집합 원소 개수 출력

JAVA

소스코드 : https://github.com/cbkpar/BOJ/blob/main/boj_1269.java

채점 번호 아이디 문제 번호 결과 메모리 시간 언어 코드 길이
30840117 cbkpar 1269 맞았습니다!! 66516KB 612ms Java 11 821B
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class Main {
	
	public static void main(String args[]) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int a,b,i,s;
		HashMap<Integer,Boolean> map = new HashMap<>();
		StringTokenizer st = new StringTokenizer(br.readLine());
		s = a = Integer.parseInt(st.nextToken());
		b = Integer.parseInt(st.nextToken());
		st = new StringTokenizer(br.readLine());
		for(i=0;i<a;i++) map.put(Integer.parseInt(st.nextToken()),true);
		st = new StringTokenizer(br.readLine());
		for(i=0;i<b;i++) {
			if(map.containsKey(Integer.parseInt(st.nextToken()))) {
				s--;
			}else {
				s++;
			}
		}
		System.out.println(s);
	}
}

S의 값을 집합 A의 개수로 초기값을 둔 뒤

HashMap을 이용하여 A의 원소를 모두 넣고

 

B의 원소를 차례대로 확인하며

HashMap에 해당 키가 있을 경우 S를 1 감소시키고

HashMap에 해당 키가 없을 경우 S를 1 증가시킨다.

 

예제 )

A = {1, 2, 4}

B = {2, 3, 4, 5, 6}

 

 

+ Recent posts