728x90
https://www.acmicpc.net/problem/20310
풀이
0은 뒤에서부터 절반만큼 제거해주고,
1은 앞에서부터 절반만큼 제거해주면 된다.
전체코드
package 백준renew;
import java.io.*;
import java.util.*;
public class 실버3_20301_타노스 {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
PriorityQueue<String> pq = new PriorityQueue<>(new Comparator<>() {
public int compare(String o1, String o2) {
return 0;
}
});
StringBuilder sb = new StringBuilder();
String word = br.readLine();
char arr[] = word.toCharArray();
int cnt[] = new int[2];
for(int i=0; i<word.length(); i++) {
if(word.charAt(i) == '0') {
cnt[0]++;
}else {
cnt[1]++;
}
}
cnt[0] /= 2;
cnt[1] /= 2;
int c = 0;
for(int i=word.length()-1; i>0; i--) {
if(arr[i] == '0') {
c++;
arr[i] = '2';
if(c == cnt[0]) {
break;
}
}else {
continue;
}
}
int n = 0;
for(int i=0; i<word.length(); i++) {
if(arr[i] == '1') {
n++;
arr[i] = '2';
if(n == cnt[1]) {
break;
}
}else {
continue;
}
}
String ans = "";
for(int i=0; i<word.length(); i++) {
if(arr[i] == '2') {
continue;
}else {
ans += arr[i];
}
}
sb.append(ans);
bw.write(sb.toString());
bw.close();
}
}
728x90
'코테 > Algorithm' 카테고리의 다른 글
[BOJ] 1141: 접두사 (JAVA) (1) | 2024.05.31 |
---|---|
[Programmers] 모음사전 (JAVA) (0) | 2024.05.29 |
[Programmers] 구명보트 (JAVA) (0) | 2024.05.22 |
[Programmers] 다음 큰 숫자 (JAVA) (0) | 2024.05.21 |
[Programmers] 이모티콘 할인행사 (JAVA) (0) | 2024.05.18 |