ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 2011๋ฒˆ - ์•”ํ˜ธ์ฝ”๋“œ

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 7. 17. 21:55

[๋ฌธ์ œ]

https://www.acmicpc.net/problem/2011

 

2011๋ฒˆ: ์•”ํ˜ธ์ฝ”๋“œ

๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ํ•ด์„์˜ ๊ฐ€์ง“์ˆ˜๋ฅผ ๊ตฌํ•˜์‹œ์˜ค. ์ •๋‹ต์ด ๋งค์šฐ ํด ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, 1000000์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์•”ํ˜ธ๊ฐ€ ์ž˜๋ชป๋˜์–ด ์•”ํ˜ธ๋ฅผ ํ•ด์„ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

[ํ’€์ด]

DP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์ดํ•˜๋Š” ๋ฌธ์ œ๋กœ, ํ•ด๋‹น ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

https://happybplus.tistory.com/45

 

[๋ฐฑ์ค€, BOJ 2011] ์•”ํ˜ธ์ฝ”๋“œ (java)

์ถœ์ฒ˜-https://www.acmicpc.net/problem/2011 2011๋ฒˆ: ์•”ํ˜ธ์ฝ”๋“œ ๋ฌธ์ œ ์ƒ๊ทผ์ด์™€ ์„ ์˜์ด๊ฐ€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ๋‚จ๋งค๊ฐ„์˜ ๋Œ€ํ™”๋ฅผ ๋“ฃ๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋Œ€ํ™”๋ฅผ ์„œ๋กœ ์•”ํ˜ธํ™” ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋Œ€

happybplus.tistory.com

์ˆซ์ž๊ฐ€ ๋งŒ๋“ค์–ด์งˆ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์„ ์ƒ๊ฐํ•˜๊ณ  ๊ฐ ์กฐ๊ฑด๋ณ„๋กœ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

์šฐ์„ , ๋งจ ์ฒ˜์Œ ์ˆซ์ž๊ฐ€ 0์ด๋ฉด(numbers.charAt(0)์ด 0์ผ๋•Œ) ํ•ด์„์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

๋˜ํ•œ ๋งจ ์ฒ˜์Œ ์ˆซ์ž๊ฐ€ 0์ด ์•„๋‹ˆ๊ณ , ํ˜„์žฌ ์ˆซ์ž๊ฐ€ 0์ด๋ผ๋ฉด(numbers.charAt(i)) ์ด์ „ ์ˆซ์ž์— 1 ๋˜๋Š” 2๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ 10, 20์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, 1 ๋˜๋Š” 2๊ฐ€ ์—†๋‹ค๋ฉด ํ•ด์„์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ ๋งจ ์ฒ˜์Œ ์ˆซ์ž๊ฐ€ 0์ด ์•„๋‹ˆ๊ณ , ๋˜ํ•œ ํ˜„์žฌ ์ˆซ์ž๋„ 0์ด ์•„๋‹ˆ๋ผ๋ฉด ํ˜„์žฌ index์˜ ์ด์ „์ˆซ์ž(numbers.charAt(i-1))์˜ ๊ฐ’์— ๋”ฐ๋ผ 3๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ด์ „ ์ˆซ์ž๊ฐ€ 1์ด๋ผ๋ฉด dp[i] = dp[i-1] + dp[i-2]
  • ์ด์ „ ์ˆซ์ž๊ฐ€ 2๋ผ๋ฉด ํ˜„์žฌ ์ˆซ์ž๊ฐ€ 1 ~ 6 ์ผ๋•Œ๋งŒ 26๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ dp[i] = dp[i-1] + dp[i-1];
  • ์œ„์˜ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•œ ์ˆซ์ž๋“ค์€ ๋ชจ๋‘ ๋‹จ๋…์ฒ˜๋ฆฌ ๋Œ€์ƒ์ด๋ฏ€๋กœ dp[i] = dp[i-1];

์ด๋•Œ, ๋งŒ์•ฝ ํ˜„์žฌ index๊ฐ€ 1์ด๋ผ๋ฉด, dp[i-2]๋Œ€์‹  1์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค.

 

 

์˜ˆ์ œ1์„ ์˜ˆ์‹œ๋กœ ์„ค๋ช…ํ•ด๋ณด์ž๋ฉด,


์ˆซ์ž๋“ค์ด 2 5 1 1 4 ๊ฐ€ ๋“ค์–ด์˜ด.

0 -> 2
1 -> 25 2+5
2 -> 25+1, 2+5+1
3 -> 25+11, 2+5+11, 25+1+1, 2+5+1+1
4 -> 25+11+4 2+5+11+4, 25+1+1+4, 2+5+1+1+4,25+1+14, 2+5+1+14

0๋ฒˆ์งธ index์˜ ๊ฐ’์€ 2 ์ด๋ฏ€๋กœ, dp[0] = 1์ž…๋‹ˆ๋‹ค.

1๋ฒˆ์งธ index์˜ ์ด์ „ ๊ฐ’์€ 2 ์ด๋ฏ€๋กœ dp[1] = 2์ž…๋‹ˆ๋‹ค.

2๋ฒˆ์งธ index์˜ ์ด์ „๊ฐ’์€ 5์ด๋ฏ€๋กœ, ๋‹จ๋…์ฒ˜๋ฆฌ ๋Œ€์ƒ์ด๋ผ 25์™€ 2+5์— ๊ฐ๊ฐ 1์„ ๋‹จ๋…์œผ๋กœ ๋”ํ•ด dp[2] = 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

3๋ฒˆ์งธ index์˜ ์ด์ „๊ฐ’์€ 1์ด๋ฏ€๋กœ, index-2์˜ ๊ฐ’๋“ค๊ณผ๋„ ๊ฒฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 25์™€ 2+5์— ๊ฐ๊ฐ 11์„ ๋”ํ•œ ์ฝ”๋“œ์™€ 25+1๊ณผ 2+5+1์— ๋‹จ๋…์œผ๋กœ 1์„ ๋”ํ•œ ์ฝ”๋“œ ์ด 4๊ฐ€์ง€๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. (dp[3] = dp[2] + dp[1] = 4)

4๋ฒˆ์งธ index๋Š” ์ด์ „๊ฐ’์„ 1๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ, 3๋ฒˆ๊ณผ ๊ฐ™์ด dp[4] = dp[3] + dp[2] = 6์ด ๋ฉ๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฐฉ๋ฒ•์€ 6๊ฐ€์ง€๋กœ dp[4]๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

[์ฝ”๋“œ]

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

public class Main {
    static int N;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String numbers = br.readLine();
        N = numbers.length();
        int[] dp = new int[N];
        decode(numbers, dp);
        System.out.println(dp[N-1]);
    }
    public static void decode(String numbers, int[] dp) {
        if(numbers.charAt(0) == '0') {
            // ์ฒซ๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ 0์ด๋ฉด ํ•ด์„ ๋ถˆ๊ฐ€๋Šฅ
            return;
        }
        dp[0] = 1;
        for(int i=1;i<numbers.length();i++) {
            // numbers[i]๊ฐ€ 0์ผ๋•Œ๋Š” ์•ž์ž๋ฆฌ๊ฐ€ 1 || 2์ผ๋•Œ๋งŒ ํ•ด์„์ด ๊ฐ€๋Šฅํ•จ
            // ๋ฌด์กฐ๊ฑด numbers[i-1]๊ณผ numbers[i]๊ฐ€ ํ•œ ์„ธํŠธ์ด๋ฏ€๋กœ, dp[i-2]๋ž‘ ๊ฐ™์€๊ฐ’.
            if(numbers.charAt(i) == '0') {
                if(numbers.charAt(i-1) != '1' && numbers.charAt(i-1) != '2') {
                    // ์ž˜๋ชป๋œ ์ฝ”๋“œ์ž„
                    return;
                }
                if(i == 1) {
                    dp[i] = 1;
                } else {
                    dp[i] = (dp[i] + dp[i-2])%1000000;
                }
            } else {
                // ์ด์ „ ์ˆซ์ž๊ฐ€ 1์ธ ๊ฒฝ์šฐ
                if(numbers.charAt(i-1) == '1') {
                    if(i == 1) {
                        dp[i] = (dp[i-1] + 1)%1000000;
                    } else {
                        dp[i] = (dp[i-1] + dp[i-2])%1000000;
                    }
                } else if(numbers.charAt(i-1) == '2' && numbers.charAt(i) >= '1' && numbers.charAt(i) <= '6') {
                    if(i == 1) {
                        dp[i] = (dp[i-1] + 1)%1000000;
                    } else {
                        dp[i] = (dp[i-1] + dp[i-2])%1000000;
                    }
                } else {
                    // ๋ฌด์ €๊ฑด ๋‹จ๋…์œผ๋กœ ์ฒ˜๋ฆฌ
                    dp[i] = dp[i-1];
                }
            }
        }
    }
}