๊ธฐ๋ก๋ฐฉ

BOJ_13699 : ์ ํ™”์‹ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_13699 : ์ ํ™”์‹

Soom_1n 2022. 11. 29. 18:29

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

13699๋ฒˆ: ์ ํ™”์‹

๋‹ค์Œ์˜ ์ ํ™”์‹์— ์˜ํ•ด ์ •์˜๋œ ์ˆ˜์—ด t(n)์„ ์ƒ๊ฐํ•˜์ž: t(0)=1 t(n)=t(0)*t(n-1)+t(1)*t(n-2)+...+t(n-1)*t(0) ์ด ์ •์˜์— ๋”ฐ๋ฅด๋ฉด, t(1)=t(0)*t(0)=1 t(2)=t(0)*t(1)+t(1)*t(0)=2 t(3)=t(0)*t(2)+t(1)*t(1)+t(2)*t(0)=5 ... ์ฃผ์–ด์ง„ ์ž…๋ ฅ 0 ≤ n

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ ํ™”์‹์œผ๋กœ ๋ฐฐ์—ด์„ ์ฑ„์šฐ๋Š” dp ๋ฌธ์ œ์ด๋‹ค.
    • arr[0] = 1
    • arr[n] = arr[0]*arr[n-1] + arr[1]*arr[n-2] + ... + arr[n-1]*arr[0]

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] arr = new long[n+1];
        arr[0] = 1;

        for (int i = 1; i <= n; i++) {
            arr[i] = 0;
            for (int j = 0; j < i; j++) {
                arr[i] += arr[j]*arr[i-1-j];
            }
        }
        System.out.println(arr[n]);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • dp๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 0~n ๊นŒ์ง€์ด๋ฏ€๋กœ n+1๊ฐœ๋กœ ์„ ์–ธํ•œ๋‹ค.
    • ์ž๋ฃŒํ˜•์€ ์˜ˆ์ œ 2๋ฒˆ์˜ ์ถœ๋ ฅ์ด 4861946401452์œผ๋กœ int๋ฅผ ์ดˆ๊ณผํ•˜๋ฏ€๋กœ long์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.
  • ์ฑ„์›Œ์•ผ ํ•˜๋Š” dp๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” 1๋ถ€ํ„ฐ n๊นŒ์ง€์ด๋ฏ€๋กœ ์ฒซ ๋ฐ˜๋ณต๋ฌธ์—์„œ i๋ฅผ 1~n๋กœ ์ˆœํšŒํ•œ๋‹ค.
    • ๋‹ค์Œ ํ•œ ์ธ๋ฑ์Šค(arr[i])๋ฅผ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•์€ i๋ฒˆ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
    • arr[i]์˜ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ 0์„ ๋„ฃ๋Š”๋‹ค.
    • j๋ฅผ 0~i-1 ๋กœ i๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ dp๋ฐฐ์—ด์˜ ๊ฐ’์„ ์ฑ„์šด๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • dp๋ฌธ์ œ์˜ ์ ํ™”์‹์„ ์ž˜๋ชป ์ดํ•ดํ•ด์„œ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๋œฏ์–ด๊ณ ์ณ์„œ ํ’€์—ˆ๋‹ค. ๋ฌธ์ œ ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์•˜๋‹ค.

728x90