https://www.acmicpc.net/problem/9625
9625번: BABBA
상근이는 길을 걷다가 신기한 기계를 발견했다. 기계는 매우 매우 큰 화면과 버튼 하나로 이루어져 있다. 기계를 발견했을 때, 화면에는 A만 표시되어져 있었다. 버튼을 누르니 글자가 B로 변했
www.acmicpc.net
저는 DP를 이용하여 문제를 해결하였습니다.
A -> B
B -> BA
이렇게 되므로 점화식을
a[n] = b[n-1]
b[n] = a[n-1] + b[n-1]
이렇게 세워서 해결하였습니다.
package BOJ.DP;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.function.Function;
public class BOJ_9625 {
static void cal(int n,int[] a, int[] b){
if(a[n] != -1){
return ;
}
cal(n-1,a,b);
a[n] = b[n-1];
b[n] = a[n-1] + b[n-1];
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(br.readLine());
int[] a = new int[n+1];
int[] b = new int[n+1];
Arrays.fill(a,-1);
Arrays.fill(b,-1);
a[0] = 1;
a[1] = 0;
b[0] = 0;
b[1] = 1;
cal(n,a,b);
System.out.println(a[n]+" "+b[n]);
}
}
'알고리즘' 카테고리의 다른 글
백준 1253번 좋다 (JAVA) (0) | 2022.01.24 |
---|---|
백준 16948번 데스 나이트 (JAVA) (0) | 2022.01.23 |
백준 1963번 소수 경로 (JAVA) (0) | 2022.01.21 |
백준 13418번 학교 탐방하기 (JAVA) (0) | 2022.01.20 |
백준 1949번 우수마을 (JAVA) (0) | 2022.01.19 |