컴퓨터 공학 기초/알고리즘 (구현)
백준 11723 ( 집합 )
JongSeok_12
2020. 8. 20. 17:22
https://www.acmicpc.net/problem/11723
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
해당 문제는 주어지는 명령의 조건에 따라 처리해주면서 알맞게 출력만 해주는 문제이다.
이때 한가지 주의해야 할 점이 있는데 일반 cin, cout 사용시 시간초과가 발생함으로 printf 와 scanf 사용 혹은
ios_base::sync_with_stdio(false);
cin.tie(NULL);
위의 구문을 사용하여 입출력을 사용한다.
풀이 :
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int n;
int nList[21];
int allList[21] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
int empList[21];
void getData(string cmd, int num){
if (cmd == "add"){
if (nList[num] == 0) nList[num] = 1;
}
else if (cmd == "remove"){
if (nList[num] == 1) nList[num] = 0;
}
else if (cmd == "check"){
if (nList[num] == 0) cout << '0' << '\n';
else cout << '1' << '\n';
}
else if (cmd == "toggle"){
if (nList[num] == 0) nList[num] = 1;
else nList[num] = 0;
}
else if (cmd == "all"){
memcpy(nList, allList, 4 * 21);
}
else if (cmd == "empty"){
memcpy(nList, empList, 4 * 21);
}
}
int main(){
//freopen_s(new FILE*, "tes.text", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
string cmd;
int num;
for (int i = 0; i < n; ++i){
cin >> cmd;
if (cmd == "add" || cmd == "check" || cmd == "remove" || cmd == "toggle"){
cin >> num;
getData(cmd, num);
}
else{
getData(cmd, -1);
}
}
}