๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 25497_๊ธฐ์ˆ  ์—ฐ๊ณ„๋งˆ์Šคํ„ฐ ์ž„์Šค

๋ฟŒ์•ผ._. 2023. 8. 29. 22:15

Silver V

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/25497)

< ๊ธฐ์ˆ  ์—ฐ๊ณ„๋งˆ์Šคํ„ฐ ์ž„์Šค >

 

๋ฌธ์ œ ํ’€์ด 

 

Stack์„ 2๊ฐœ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

ํ•˜๋‚˜๋Š” R์˜ ์‚ฌ์ „ ๊ธฐ์ˆ ์ธ L์„ ์ €์žฅํ•˜๊ณ , ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” K์˜ ์‚ฌ์ „ ๊ธฐ์ˆ ์ธ S๋ฅผ ์ €์žฅํ•œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class _25497_ { // ๊ธฐ์ˆ  ์—ฐ๊ณ„๋งˆ์Šคํ„ฐ ์ž„์Šค

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(bf.readLine());
		String str = bf.readLine();

		Stack<Character> sStack = new Stack<>();
		Stack<Character> lStack = new Stack<>();
		int result = 0;

		for (int i = 0; i < n; i++) {
			char x = str.charAt(i);
			if (Character.isDigit(x)) {
				result += 1;
			} else if (x == 'L') {
				lStack.add(x);
			} else if (x == 'S') {
				sStack.add(x);
			} else if (x == 'R') {
				if (!lStack.isEmpty()) {
					lStack.pop();
					result += 1;
				} else {
					break;
				}
			} else if (x == 'K') {
				if (!sStack.isEmpty()) {
					sStack.pop();
					result += 1;
				} else {
					break;
				}
			}
		}
		System.out.println(result);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ๊ธฐ์ˆ  ์‚ฌ์šฉ ํšŸ์ˆ˜
str : ๊ธฐ์ˆ 
sStack : Stack <Character>
LStack : Stack <Character>
result : ์ •์ƒ์ ์œผ๋กœ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•œ ์ด ํšŸ์ˆ˜

 

- ์ด ๊ธฐ์ˆ  ์‚ฌ์šฉ ํšŸ์ˆ˜(n) ์ž…๋ ฅ

- ๊ธฐ์ˆ (str) ์ž…๋ ฅ

- ๊ธฐ์ˆ  ํƒ์ƒ‰

: ์ˆซ์ž(1~9)๋ผ๋ฉด ์—ฐ๊ณ„ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์ด๋ฏ€๋กœ result +1

: L์ด๋‚˜ S๋ผ๋ฉด ์‚ฌ์ „ ๊ธฐ์ˆ ์ด๋ฏ€๋กœ ๊ฐ๊ฐ stack์— ์ €์žฅ

: R์ด๋‚˜ K๋ผ๋ฉด ๋ณธ ๊ธฐ์ˆ ์ด๋ฏ€๋กœ ์‚ฌ์ „ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ–ˆ๋Š”์ง€ ํ™•์ธ ํ›„ ์‚ฌ์šฉํ–ˆ์„ ๊ฒฝ์šฐ result +1, ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ทธ ์ดํ›„ ๊ธฐ์ˆ ์€ ๋‹ค ์ •์ƒ์ ์œผ๋กœ ๋ฐœ๋™๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ข…๋ฃŒ

- ์ •์ƒ์ ์œผ๋กœ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•œ ์ด ํšŸ์ˆ˜(result) ์ถœ๋ ฅ



'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 23843_์ฝ˜์„ผํŠธ  (0) 2023.08.31
[Baekjoon] 25381_ABBC  (0) 2023.08.30
[Baekjoon] 2257_ํ™”ํ•™์‹๋Ÿ‰  (0) 2023.08.28
[Baekjoon] 28278_์Šคํƒ 2  (0) 2023.08.25
[Baekjoon] 17952_๊ณผ์ œ๋Š” ๋๋‚˜์ง€ ์•Š์•„!  (0) 2023.08.24