728x90
문제
문제 링크 : https://www.acmicpc.net/problem/11723
원리
들어온 커맨드에 따라 자료구조 메서드를 호출하면 된다.
풀이방법
1. HashSet을 이용한 방법 - 2위
: 검색에 따라 더 빠르게 나오기도 한다.
2. ArrayList를 이용한 방법 - 1위
: 순서가 있어서 그런지 가장 빠르다
3. HashMap을 이용한 방법 - 3위
: 의외로 가장 느리다
나의 코드
1. HashSet을 이용한 방법
더보기
import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class no11723 {
static Set<String> set = new HashSet<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String cmd = st.nextToken();
cmd = (cmd.equals("all") || cmd.equals("empty")) ? allOrEmpty(cmd) : calculate(cmd, st.nextToken(), bw);
}
bw.close();
}
private static String calculate(String cmd, String x, BufferedWriter bw) throws IOException {
if (cmd.equals("add")) {
if (!set.contains(x)) set.add(x);
} else if (cmd.equals("remove")) {
if (set.contains(x)) set.remove(x);
} else if (cmd.equals("check")) {
if (set.contains(x)) bw.append("1\n");
else bw.append("0\n");
} else if (cmd.equals("toggle")) {
if (set.contains(x)) set.remove(x);
else set.add(x);
}
return null;
}
private static String allOrEmpty(String cmd) throws IOException {
if (cmd.equals("all")) {
Set<String> newSet = new HashSet<>();
for (int i = 1; i <= 20; i++) newSet.add(String.valueOf(i));
set = newSet;
} else if (cmd.equals("empty")) {
set.clear();
}
return null;
}
}
2. ArrayList를 이용한 방법
더보기
import java.io.*;
import java.util.*;
public class no11723 {
static List<String> list = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String cmd = st.nextToken();
cmd = (cmd.equals("all") || cmd.equals("empty")) ? allOrEmpty(cmd) : calculate(cmd, st.nextToken(), bw);
}
bw.close();
}
private static String calculate(String cmd, String x, BufferedWriter bw) throws IOException {
if (cmd.equals("add")) {
if (!list.contains(x)) list.add(x);
} else if (cmd.equals("remove")) {
if (list.contains(x)) list.remove(x);
} else if (cmd.equals("check")) {
if (list.contains(x)) bw.append("1\n");
else bw.append("0\n");
} else if (cmd.equals("toggle")) {
if (list.contains(x)) list.remove(x);
else list.add(x);
}
return null;
}
private static String allOrEmpty(String cmd) {
if (cmd.equals("all")) {
List<String> newList = new ArrayList<>();
for (int i = 1; i <= 20; i++) newList.add(String.valueOf(i));
list = newList;
} else if (cmd.equals("empty")) {
list.clear();
}
return null;
}
}
3. HashMap을 이용한 방법
더보기
static Map<String, String> map = new HashMap();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String cmd = st.nextToken();
cmd = (cmd.equals("all") || cmd.equals("empty")) ? allOrEmpty(cmd) : calculate(cmd, st.nextToken(), bw);
}
bw.close();
}
private static String calculate(String cmd, String x, BufferedWriter bw) throws IOException {
if (cmd.equals("add")) {
if (!map.containsValue(x)) map.put(cmd+x, x);
} else if (cmd.equals("remove")) {
if (map.containsValue(x)) map.remove("add"+x);
} else if (cmd.equals("check")) {
if (map.containsValue(x)) bw.append("1\n");
else bw.append("0\n");
} else if (cmd.equals("toggle")) {
if (map.containsValue(x)) map.remove("add"+x);
else map.put("add"+x, x);
}
return null;
}
private static String allOrEmpty(String cmd) {
if (cmd.equals("all")) {
Map<String, String> newMap = new HashMap<>();
for (int i = 1; i <= 20; i++) newMap.put("add"+i, String.valueOf(i));
map = newMap;
} else if (cmd.equals("empty")) {
map.clear();
}
return null;
}
728x90
'알고리즘 저장소 (일반방식과 나만의 풀이) > JAVA' 카테고리의 다른 글
[백준] no7662: 이중 우선순위 큐 (0) | 2023.01.29 |
---|---|
[백준] no1463: 1로 만들기 (0) | 2023.01.28 |
[백준] no1003 (0) | 2023.01.26 |
[백준] no18111: 마인크래프트 (0) | 2023.01.23 |
[백준] no2292: 벌집 (0) | 2023.01.23 |