Algorithm/📖Baekjoon

#1269 대칭 차집합

yewoneeee 2022. 5. 20. 03:02

# 문제

# 입력 및 출력

# 풀이

이 문제 역시 map을 사용해서 간단하게 해결했음

#include <iostream>
#include <map>
using namespace std;
map<int, pair<int, int>> m;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int a, b, t;
	cin >> a >> b;
	while (a--) {
		cin >> t;
		m[t].first = 1;
	}
	while (b--) {
		cin >> t;
		m[t].second = 1;
		if (m[t].first) m.erase(t);
	}
	cout << m.size() << "\n";
}

우선 map의 value값은 int 쌍으로 두어 A집합에 존재하는지, B집합에 존재하는지를 표시할 수 있도록 했음

A집합에 존재한다면 map에 key, value값 넣어주고 B집합에 존재하는 key값도 마찬가지로 처리해줌

대신 A집합에도 존재하고 B집합에도 존재하는 값은 지워버림

남은 값이 바로 대칭 차집합 원소 개수임