티스토리 뷰
//알고리즘
1. 최대 20만까지인 배열이므로 완탐으로 풀면 시간초과 -> 아이디어를 요구하는 문제인듯.
2. 규칙대로 진행하면 버블정렬이 되는것을 알 수 있음.
3. W~B순으로 정렬하면 되니까 맨 뒤의 B부터 정렬할 횟수를 계산.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class 카드뒤집기 {
static int T;
static long Ans;
static char[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
for (int tc = 1; tc <= T; tc++) {
String str = br.readLine();
arr = str.toCharArray();
// 알고리즘
//BWb
//WBB
int start = 1;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] == 'B') {
Ans += (arr.length - start++ - i);
}
}
System.out.printf("#%d %d\n", tc, Ans);
Ans = 0;
}
}
}
'Algorithm' 카테고리의 다른 글
백준_백도어_9205 (0) | 2020.10.04 |
---|---|
SW Expert Academy_햄버거 다이어트(DP) (0) | 2020.10.01 |
SW Expert Academy_벌꿀 채취 (0) | 2020.09.29 |
백준_현수막_14716 (0) | 2020.09.28 |
SW Expert Academy_숫자 만들기 (0) | 2020.09.27 |