나의 브을로오그으

[c++] 11723번 : 집합 본문

알고리즘/BaekJoon

[c++] 11723번 : 집합

__jhp_+ 2022. 9. 19. 21:28

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

 

11723번: 집합

첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.

www.acmicpc.net

#include <iostream>
#include <cstring>
using namespace std;

#define DEFAULT_LENGTH		20

class Set
{
public:
	Set()
		: arr{ false, },
		length(0),
		capacity(DEFAULT_LENGTH)
	{
	}
	~Set()
	{
	}

	int add(int data)
	{
		int ins = -1;
		if (arr[data] == false)
		{
			arr[data] = true;
			ins = data;
			++length;
		}
		return ins;
	}

	int remove(int data)
	{
		int rem = -1;
		if (arr[data] == true)
		{
			arr[data] = false;
			rem = data;
			--length;
		}
		return rem;
	}

	bool check(int data)
	{
		return arr[data];
	}

	void toggle(int data)
	{
		if (arr[data] == true)
		{
			arr[data] = false;
			--length;
		}
		else
		{
			arr[data] = true;
			++length;
		}
	}

	void all()
	{
		memset(arr, 1, sizeof(arr));
		length = capacity;
	}

	void empty()
	{
		memset(arr, 0, sizeof(arr));
		length = 0;
	}

private:
	bool arr[DEFAULT_LENGTH + 1];
	int length;
	int capacity;
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	string input;
	Set S;
	int M, x;
	
	cin >> M;
	cin.ignore(256, '\n');
	for (int i = 0; i < M; ++i)
	{
		cin >> input;
		if (input == "add")
		{
			cin >> x;
			S.add(x);
		}
		else if (input == "remove")
		{
			cin >> x;
			S.remove(x);
		}
		else if (input == "check")
		{
			cin >> x;
			cout << S.check(x) << '\n';
		}
		else if (input == "toggle")
		{
			cin >> x;
			S.toggle(x);
		}
		else if (input == "all")
		{
			S.all();
		}
		else if (input == "empty")
		{
			S.empty();
		}
		else
		{
			/* Wrong Type Instruction */
		}

	}

	return 0;
}

'알고리즘 > BaekJoon' 카테고리의 다른 글

[c++] 11726번 : 2Xn 타일링  (0) 2022.09.20
[c++] 11724번 : 연결 요소의 개수  (0) 2022.09.20
[c++] 11659번 : 구간 합 구하기 4  (0) 2022.09.19
[c++] 11403번 : 경로 찾기  (0) 2022.09.19
[c++] 11399번 : ATM  (0) 2022.09.19