OJ
[BOJ] 2011 암호코드 (JAVA)
P3PP4
2023. 4. 14. 10:00
https://www.acmicpc.net/problem/2011
2011번: 암호코드
나올 수 있는 해석의 가짓수를 구하시오. 정답이 매우 클 수 있으므로, 1000000으로 나눈 나머지를 출력한다. 암호가 잘못되어 암호를 해석할 수 없는 경우에는 0을 출력한다.
www.acmicpc.net
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String password = br.readLine();
int len = password.length();
int[] val = new int[len + 1];
val[0] = val[1] = 1;
for (int i = 2; i <= len; i++) {
val[i] = (val[i - 2] + val[i - 1]) % 1_000_000;
}
int pre = 0;
int result = 1;
int cnt = 0;
for (int i = 0; i < len; i++) {
int now = password.charAt(i) - '0';
if (1 <= now && now <= 2) {
cnt++;
} else if (3 <= now) {
if (pre == 1 || (pre == 2 && now <= 6)) cnt++;
result = (result * val[cnt]) % 1_000_000;
cnt = 0;
} else if (now == 0) {
if (pre == 0 || 3 <= pre) {
System.out.print(0);
return;
} else {
result = (result * val[--cnt]) % 1_000_000;
cnt = 0;
}
}
pre = now;
}
if (1 < cnt) result = (result * val[cnt]) % 1_000_000;
System.out.print(result);
}
}