11723번. 집합.

phoenixKim·2022년 9월 6일
0

백준 알고리즘

목록 보기
110/174

문제점.

  • 1번. 최종코드와는 다르게 string s를 입력으로 사용했는데, 시간초과 발생함.
    나머지 비트연산자 결과는 이상없었음.

  • 2번. 입력되는 번호를 -= 1 해야함.

시간 초과 발생시 변경하자.

1) endl -> '\n'
2) string -> char ,
3) cin -> scanf("%s", &word);

알고리즘 분류

: 비트연산자.

최종 코드

#include <iostream>

using namespace std;

#include <vector>
#include <algorithm>

#include <string>

// 11723번 집합.
// 15:24 ~ 15:41 7퍼센트에서 시간초과 발생함. 

char s[11];

int main(void)
{
	int n;
	cin >> n;

	int num = 0;

	// 확인용
	//vector<int>v;

	for (int i = 0; i < n; ++i)
	{
		int idx =0;
		//string s;
		//cin >> s;
		scanf("%s", &s);

		if (s[0] == 'a' &&  s[1] == 'l' || s[0] == 'e' && s[1] == 'm')
		{

		}
		else
			scanf("%d", &idx);
			//cin >> idx;

		idx -= 1;
		if (s[0] == 'a' && s[1] == 'd')
		{
			num |= (1 << idx);
		}
		else if (s[0] == 'c')
		{
			if (num & (1 << idx))
			{
				//v.push_back(1);
				cout << 1 << '\n';
			}
			else
			{
				cout << 0 << '\n';
				//v.push_back(0);

			}
				
		}
		else if (s[0] == 'r')
		{
			num &= ~(1 << idx);
		}
		else if (s[0] == 'a' && s[1] == 'l')
		{
			num = (1 << 20) - 1;			
		}
		else if (s[0] == 't')
		{
			num ^= (1 << idx);
		}
		else if (s[0] == 'e')
		{
			num = 0;
		}

	}
	
	// 확인용
	//for (auto iter : v)
	// cout << iter << endl;
}
profile
🔥🔥🔥

0개의 댓글

관련 채용 정보