๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/25381)
< ABBC >
๋ฌธ์  ํ์ด
A -> B , B -> C๋ฅผ ์ง์ฐ๊ธฐ ์ํด์๋ ์ด๋์ ์์นํ B๋ฅผ ์ง์ฐ๋๊ฐ๊ฐ ์ค์ํ๋ค๊ณ ์๊ฐํ๋ค.
A๋ฅผ ๋ค์์๋ถํฐ ์ง์ธ ๋ B ๋ํ ๋ค์์๋ถํฐ ์ง์ด๋ค.
B๋ฅผ ์์์๋ถํฐ ์ง์ธ ๋ C ๋ํ ์์์๋ถํฐ ์ง์ด๋ค.
์ด ๋ฐฉ์์ ์ฌ์ฉํด์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
public class _25381_ { // ABBC
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String str = bf.readLine();
		PriorityQueue<Integer> aQueue = new PriorityQueue<>(Collections.reverseOrder());
		Deque<Integer> bQueue = new ArrayDeque<>();
		Queue<Integer> cQueue = new LinkedList<>();
		int result = 0;
		for (int i = 0; i < str.length(); i++) {
			char x = str.charAt(i);
			if (x == 'A') {
				aQueue.add(i);
			} else if (x == 'B') {
				bQueue.add(i);
			} else if (x == 'C') {
				cQueue.add(i);
			}
		}
		while (!aQueue.isEmpty()) {
			int num = aQueue.poll();
			if (!bQueue.isEmpty() && num < bQueue.peekLast()) {
				result += 1;
				bQueue.pollLast();
			}
		}
		while (!bQueue.isEmpty()) {
			int num = bQueue.pollFirst();
			while(!cQueue.isEmpty() && num > cQueue.peek()) {
				cQueue.poll();
			}
			if(!cQueue.isEmpty() && num < cQueue.peek()) {
				result+=1;
				cQueue.poll();
			}
		}
		System.out.println(result);
	}
}
Main
๋ณ์)
str : ๋ฌธ์์ด
aQueue : PriorityQueue <Integer>
bQueue : Deque <Integer>
cQueue : Queue <Integer>
result : ์ํํ ์ ์๋ ์ต๋ ํ์
- ๋ฌธ์์ด(str) ์ ๋ ฅ
- ๋ฌธ์์ด ํ์ํ๋ฉด์ A, B, C์ ๊ฐ๊ฐ index๋ฅผ ์ ์ฅ
- ๋ฌธ์์ด ๋ค์ ์๋ A๋ถํฐ ์ ๊ฑฐํ๋ฉฐ B ๋ํ ๋ค์ ์๋ B๋ถํฐ ์ ๊ฑฐ
: A์ index๋ณด๋ค B์ index๊ฐ ์ปค์ผ result+1 ํ ์ ๊ฑฐ ๊ฐ๋ฅ
- ๋ฌธ์์ด ์์ ์๋ B๋ถํฐ ์ ๊ฑฐํ๋ฉฐ C ๋ํ ์์ ์๋ C๋ถํฐ ์ ๊ฑฐ
: B์ index๋ณด๋ค C์ index๊ฐ ์๋ค๋ฉด ๊ณ์ํด์ ์ ๊ฑฐํ ์ ์์ผ๋ฏ๋ก C์ index๊ฐ ๋ ์ปค์ง ๋๊น์ง ์ ๊ฑฐ
: B์ index๊ฐ C์ index๋ณด๋ค ์๋ค๋ฉด result+1 ํ C ์ ๊ฑฐ
- ์ํํ ์ ์๋ ์ต๋ ํ์(result) ์ถ๋ ฅ

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Baekjoon] 1976_์ฌํ ๊ฐ์ (0) | 2023.09.01 | 
|---|---|
| [Baekjoon] 23843_์ฝ์ผํธ (0) | 2023.08.31 | 
| [Baekjoon] 25497_๊ธฐ์ ์ฐ๊ณ๋ง์คํฐ ์์ค (0) | 2023.08.29 | 
| [Baekjoon] 2257_ํํ์๋ (0) | 2023.08.28 | 
| [Baekjoon] 28278_์คํ 2 (0) | 2023.08.25 |