Baekjoon 2011

DP

Baekjoon 2011

Description

상근이와 선영이가 다른 사람들이 남매간의 대화를 듣는 것을 방지하기 위해서 대화를 서로 암호화 하기로 했다. 그래서 다음과 같은 대화를 했다.

상근: 그냥 간단히 암호화 하자. A를 1이라고 하고, B는 2로, 그리고 Z는 26으로 하는거야.
선영: 그럼 안돼. 만약, “BEAN”을 암호화하면 25114가 나오는데, 이걸 다시 글자로 바꾸는 방법은 여러 가지가 있어.
상근: 그렇네. 25114를 다시 영어로 바꾸면, “BEAAD”, “YAAD”, “YAN”, “YKD”, “BEKD”, “BEAN” 총 6가지가 나오는데, BEAN이 맞는 단어라는건 쉽게 알수 있잖아?
선영: 예가 적절하지 않았네 ㅠㅠ 만약 내가 500자리 글자를 암호화 했다고 해봐. 그 때는 나올 수 있는 해석이 정말 많은데, 그걸 언제 다해봐?
상근: 얼마나 많은데?
선영: 구해보자!
어떤 암호가 주어졌을 때, 그 암호의 해석이 몇 가지가 나올 수 있는지 구하는 프로그램을 작성하시오.

Input

첫째 줄에 5000자리 이하의 암호가 주어진다. 암호는 숫자로 이루어져 있다.

Output

나올 수 있는 해석의 가짓수를 구하시오. 정답이 매우 클 수 있으므로, 1000000으로 나눈 나머지를 출력한다.

암호가 잘못되어 암호를 해석할 수 없는 경우에는 0을 출력한다.

Example I & O

Input 1
25114

Output 1
6

Input 2
1111111111

Output 2
89

Input 3
015658946146

Output 3
0

Input 4
2020

Output 4
1

Input 5
16989815616001568496

Output 5
0

My solution

password = [0]
string = input()

for s in string:
    password.append(int(s))

dp = [0] * (len(password))
dp[0] = 1
dp[1] = 1

for i in range(2, len(password)):
    if password[i] < 0:
        dp[-1] = 0
        break
    if password[i] == 0:
        if password[i - 1] == 0:
            dp[-1] = 0
            break
        if password[i - 1] == 1 or password[i - 1] == 2:
            dp[i] = dp[i - 2] % 1000000
            continue
        else:
            dp[i] = 0
            break

    if password[i - 1] == 1:
        dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000
    elif password[i - 1] == 2:
        if password[i] <= 6:
            dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000
        else:
            dp[i] = dp[i - 1] % 1000000
    else:
        dp[i] = dp[i - 1] % 1000000


if string[0] == "0":
    print(0)
else:        
    print(dp[-1])
  • Sketch 그림1

    General Recurrence relation
    1~26 (‘i-1’ + ‘i’): dp[i] = dp[i - 1] (one digit) + dp[i - 2] (two digits)
    else : dp[i] = dp[i - 1]

    그림2

  • Code Analysis

password = [0]
string = input()

for s in string:
    password.append(int(s))

dp = [0] * (len(password))
dp[0] = 1
dp[1] = 1

password : ex. 25114 » [0, 2, 5, 1, 1, 4]
formation of dp (default: dp[0] = 1, dp[1] = 1)


for i in range(2, len(password)):
    if password[i] < 0:
        dp[-1] = 0
        break
    if password[i] == 0:
        if password[i - 1] == 0:
            dp[-1] = 0
            break
        if password[i - 1] == 1 or password[i - 1] == 2:
            dp[i] = dp[i - 2] % 1000000
            continue
        else:
            dp[i] = 0
            break

password included negative value : 0
password included “00” : 0
password included 10 or 20 : only possible two digits → Consequently, dp[i] = dp[i - 2]
password included 30 40 50 … : 0


if password[i - 1] == 1:
    dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000
elif password[i - 1] == 2:
    if password[i] <= 6:
        dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000
    else:
        dp[i] = dp[i - 1] % 1000000
else:
    dp[i] = dp[i - 1] % 1000000

General Recurrence relation


if string[0] == "0":
    print(0)
else:        
    print(dp[-1])

password starting 0 : 0
print the result

Best answer

answer


© 2017. All rights reserved.

Powered by Hydejack v조현진