본문 바로가기

Algorithm/Baekjoon BOJ

[백준][C++] 1269 : 대칭 차집합

반응형

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

 

1269번: 대칭 차집합

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

www.acmicpc.net

 

 

 

 


- 문제

 

  두 집합이 주어지면 두 집합의 차집합을 모두 더하는 문제다.

 

 


- 해설

 

  이건 map으로 풀어야 한다는 생각 없이 그냥 풀었다가 진짜 브론즈 문제인것 처럼 find를 이용해 차집합을 구했다.

 

다른 사람들이 푼 거 중에서 가장 잘 만들었고 내가 만약 map으로 풀었으면 이렇게 풀었을 것 같은 걸 같이 첨부하겠다.

 

 


- 풀이

#include <iostream>
#include <set>

using namespace std;

#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

int main()
{
    fastio;
    int N, M, temp;
    set<int> A, B;

    cin >> N >> M;

    for (int i = 0; i < N; i++)
    {
        cin >> temp;
        A.insert(temp);
    }
    for (int i = 0; i < M; i++)
    {
        cin >> temp;
        B.insert(temp);
    }
    for (auto i : A)
    {
        if (B.find(i) != B.end())
            N--;
    }
    for (auto i : B)
    {
        if (A.find(i) != A.end())
            M--;
    }
    cout << N + M;
    return 0;
}

 

 

#include <iostream>
#include <map>
using namespace std;
map<int, bool> m;
int N,M;
int num;
int main(void)
{
	cin >> N >> M;
	for (int i = 0; i < N+M; i++)
	{
		cin >> num;
		if (m[num] == true) 
			m.erase(num);
		else 
			m[num] = true;
	}
	cout << m.size();

}

 


- 새롭게 알게 된 점

 

X

반응형