알고리즘 문제풀이
[백준/BOJ] 백준 1269번 : 대칭 차집합
GeniusJo
2020. 9. 23. 02:25
1269번: 대칭 차집합
첫째 줄에 집합 A의 원소의 개수와 집합 B의 원소의 개수가 빈 칸을 사이에 두고 주어진다. 둘째 줄에는 집합 A의 모든 원소가, 셋째 줄에는 집합 B의 모든 원소가 빈 칸을 사이에 두고 각각 주어��
www.acmicpc.net
set을 이용하여 중복된 값을 판단해 문제를 해결했다.
코드
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int a_num, b_num;
int input;
set<int> a_result; //A-B
set<int> b_result; //B-A
set<int>::iterator it;
int result;
cin >> a_num >> b_num;
for (int i = 0; i < a_num; i++)
{
cin >> input;
a_result.insert(input); //일단 저장한다
}
for (int i = 0; i < b_num; i++)
{
cin >> input;
it = a_result.find(input);
if (it != a_result.end()) //input이 a_result에 저장되어 있다면
{
a_result.erase(it); //a_result에서 해당 값을 제거한다
}
else
b_result.insert(input); //a_result에 없는 값이라면 b_result에 저장한다
}
result = a_result.size() + b_result.size();
cout << result;
return 0;
}