Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- BOJ
- DP
- 입실 퇴실
- ElementTree
- 복서 정렬하기
- 위클리 챌린지
- 너비우선탐색
- 위클리 6주차
- 완전 탐색
- 문서자동화
- 그래프
- 프로그래머스
- 코딩테스트
- 좋은 수열
- 재귀
- dfs
- 39080
- 줄어드는 숫자
- BFS
- 백트랙킹
- Java
- 부분 수열의 합
- 백트래킹
- 몯느 순열
- 1174
- 순열장난
- 백준
- 백트렉킹
- openssl
- 10597
Archives
개발자-H 입니다.
BOJ - 집합 본문
https://www.acmicpc.net/problem/11723
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
- 비트마스킹을 활용한 문제이다.
- 문제의 입력 수가 3,000,000 이기에 System.out.println 함수를 사용 할 경우 시간 초과가 날 수 있다.
- StringBuilder 를 활용하면 된다!
import java.io.*;
import java.util.*;
public class Main {
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int bit = 0;
private static StringBuilder builder = new StringBuilder();
public static void main(String[] args) throws Exception {
int M = Integer.parseInt(br.readLine());
StringTokenizer st;
while (M-- > 0) {
st = new StringTokenizer(br.readLine());
String command = st.nextToken();
switch(command) {
case "add":
add(Integer.parseInt(st.nextToken()));
break;
case "remove":
remove(Integer.parseInt(st.nextToken()));
break;
case "check":
check(Integer.parseInt(st.nextToken()));
break;
case "toggle":
toggle(Integer.parseInt(st.nextToken()));
break;
case "all":
bit = 0x1FFFFE;
break;
case "empty":
bit = 0;
break;
}
}
System.out.println(builder);
}
public static void add(int position) {
bit |= (0x1 << position);
}
public static void remove(int position) {
bit &= ~(0x1 << position);
}
public static void check(int position) {
builder.append(((bit & (0x1 << position)) >> position) + "\n");
}
public static void toggle(int position) {
bit ^= (0x1 << position);
}
}
'Java' 카테고리의 다른 글
JVM -Dfile.encoding 옵션과 문자열 변화 비교 (0) | 2021.02.04 |
---|
Comments