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 |
Tags
- 다익스트라
- 스택
- 비트마스킹
- 이분 탐색
- 메모이제이션
- 게이트웨이
- 구현
- 플로이드 와샬
- 스프링 시큐리티
- Zuul
- 이분 매칭
- BFS
- dp
- 구간 트리
- spring boot
- 주울
- ZuulFilter
- 서비스 디스커버리
- 트리
- 유레카
- Logback
- spring cloud
- Java
- 완전 탐색
- Gradle
- Spring Cloud Config
- 백트래킹
- docker-compose
- 도커
- 달팽이
Archives
- Today
- Total
Hello, Freakin world!
[백준 11723번][Java] 집합 - 비트마스크 집합 연산 구현하기 본문
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
집합 요소를 삭제하는 연산의 구현이 조금 까다로운 면이 있다.
이를 구현하기 위해 모든 집합의 요소를 포함하는 111.. 11와 같이 1로 완전히 채워진 비트를 만들고, 지우려는 요소 위치의 원소를 XOR 연산을 통해 0으로 만들어 준다. 그리고 집합 S와 and 연산을 해주면 해당 요소가 지워진 집합을 얻을 수 있다.
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
static int m, S=0;
public static void main(String[] args) throws IOException {
InputReader reader = new InputReader();
m = reader.readInt();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < m; i++) {
StringTokenizer st = new StringTokenizer(reader.readLine());
String command = st.nextToken();
if(st.hasMoreTokens()) {
int n = Integer.parseInt(st.nextToken());
if(command.equals("add")) {
add(n);
}
if(command.equals("remove")) {
remove(n);
}
if(command.equals("check")) {
sb.append(check(n) + "\n");
}
if(command.equals("toggle")) {
toggle(n);
}
}
if(command.equals("all")) {
all();
}
if(command.equals("empty")) {
empty();
}
}
if(sb.length() > 0) sb.deleteCharAt(sb.length()-1);
System.out.println(sb.toString());
}
private static void empty() {
S = 0;
}
private static void all() {
S = (1 << 20) - 1;
}
private static void toggle(int n) {
if(check(n) == 0) add(n);
else remove(n);
}
private static int check(int n) {
if((S & (1 << n-1)) == 0) return 0;
return 1;
}
private static void remove(int n) {
int all = (1 << 20)-1;
int comp = all ^ (1 << n-1);
S &= comp;
}
private static void add(int n) {
S |= (1 << n-1);
}
}
class InputReader {
private BufferedReader br;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public InputReader(String filepath) {
try {
br = new BufferedReader(new FileReader(filepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public List<Character> readLineIntoCharList() throws IOException {
List<Character> l = new ArrayList<>();
while(true) {
int readVal = br.read();
if(readVal == '\n' || readVal == -1) break;
l.add((char)readVal);
}
return l;
}
public boolean ready() throws IOException {
return br.ready();
}
public String readLine() throws IOException {
return br.readLine();
}
public int readInt() throws IOException {
return Integer.parseInt(readLine());
}
public Long readLong() throws IOException {
return Long.parseLong(readLine());
}
}
'알고리즘 > PS' 카테고리의 다른 글
[백준 2875번][Java] 대회 or 인턴 - 간단한 방정식 (0) | 2020.10.04 |
---|---|
[백준 1525번][Java] 퍼즐 - 그래프 상태 비트마스킹하기 (0) | 2020.10.04 |
[백준 2357번][Java] 최솟값과 최댓값 - 구간 트리, 람다의 성능 (0) | 2020.09.29 |
[백준 7562번][Java] 나이트의 이동 (0) | 2020.09.29 |
[백준 14888번][Java] 연산자 끼워넣기 - 완전탐색, 백트래킹 (0) | 2020.09.27 |
Comments