Skip to content

Commit

Permalink
2024-07-12 17:15:23
Browse files Browse the repository at this point in the history
Affected files:
.obsidian/workspace.json
src/content/blog/boj-2011-암호코드.md
  • Loading branch information
gyunseo committed Jul 12, 2024
1 parent ac11a8d commit 810fe53
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
24 changes: 12 additions & 12 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"type": "split",
"children": [
{
"id": "1f6f8d12bcf5caf1",
"id": "9cd648d7251dacb7",
"type": "tabs",
"children": [
{
"id": "e6eed48a7bf97d28",
"id": "12588c25489580ac",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md",
"file": "src/content/blog/boj-2529-부등호.md",
"mode": "source",
"source": false
}
Expand All @@ -22,16 +22,16 @@
]
},
{
"id": "b805cde4ef212b32",
"id": "e69a48971a0a2c37",
"type": "tabs",
"children": [
{
"id": "fdd86d334e7ba6e3",
"id": "8e00635bfc168bc5",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/boj-5525-ioioi.md",
"file": "src/content/blog/boj-2011-암호코드.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -103,7 +103,7 @@
"state": {
"type": "backlink",
"state": {
"file": "src/content/blog/boj-5525-ioioi.md",
"file": "src/content/blog/boj-2011-암호코드.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -120,7 +120,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "src/content/blog/boj-5525-ioioi.md",
"file": "src/content/blog/boj-2011-암호코드.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -143,7 +143,7 @@
"state": {
"type": "outline",
"state": {
"file": "src/content/blog/boj-5525-ioioi.md"
"file": "src/content/blog/boj-2011-암호코드.md"
}
}
}
Expand All @@ -166,16 +166,17 @@
"table-editor-obsidian:Advanced Tables Toolbar": false
}
},
"active": "fdd86d334e7ba6e3",
"active": "8e00635bfc168bc5",
"lastOpenFiles": [
"src/content/blog/boj-2529-부등호.md",
"src/content/blog/boj-2011-암호코드.md",
"src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md",
"src/content/blog/boj-5525-ioioi.md",
"src/content/blog/leet-code-74-search-a-2d-matrix.md",
"src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md",
"src/content/blog/boj-2660-회장뽑기.md",
"src/content/blog/leet-code-64-minimum-path-sum.md",
"src/content/blog/boj-6593-상범-빌딩.md",
"src/content/blog/boj-2529-부등호.md",
"src/content/blog/왜-node에서-pino-logger를-이용한-log-io는-cpu-usage에-큰-영향이-없을까.md",
"src/content/blog/.md",
"src/content/blog/boj-1309-동물원.md",
Expand All @@ -195,7 +196,6 @@
"src/content/blog/implementing-a-worker-thread-pool-in-c.md",
"src/content/blog/boj-5014-스타트링크.md",
"src/content/blog/boj-random-defense.md",
"src/content/blog/boj-14425-문자열-집합.md",
"dist/assets/[email protected]",
"dist/assets/forrest-gump-quote.webp.jpg",
"dist/_astro/[email protected]",
Expand Down
72 changes: 72 additions & 0 deletions src/content/blog/boj-2011-암호코드.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
author: Gyunseo Lee
title: "BOJ 백준 2011: 암호코드"
pubDatetime: 2024-07-12T17:10:00+09:00
modDatetime: 2024-07-12T17:10:00+09:00
featured: false
draft: false
tags:
- PS
- BOJ
- Algorithms
- 실랜디
- 골랜디
- DP
description: 점화식! 점화식!
ogImage: ""
---

## Table of contents

## 들어가며

걸린 시간: 41분

이 문제를 처음 읽고 든 생각은 다음과 같았습니다.

> 아 이거 알고리즘 강의 시간에 풀었었는데...
근데 접근 방식과 풀이 방법은 전혀 기억이 나지 않았고, 일단 "가지수"를 세는 것이길래 일단 DP를 의심하면서, 개수를 천천히 세어 나갔습니다.

## 접근

![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1720772016/image_gmspan.png)

점화식의 핵심은 내가 현재 보고 있는 이 문자열이 따로 톡 떼놓았을 때 valid한가? 그리고 이전 문자열과 합쳤을 때 또 valid한가?였습니다.
왜냐면 암호코드는 1 ~26까지 범위에만 있으니깐요 ㅎㅎ

## 구현

```python
import sys

input = sys.stdin.readline

if __name__ == "__main__":
MOD = 1000_000
S = input().strip()
dp = [[0 for __ in range(2)] for _ in range(len(S))]
dp[0][0] = 1 if S[0] != "0" else 0
validJoinString = ["11","12","13","14","15","16","17","18","19","21","22","23","24","25","26"]
for i in range(1, len(S)):
if S[i] == "0":
# 이전 문자와 결합하지 않는 경우
dp[i][0] = 0
# 이전 문자와 결합하는 경우
dp[i][1] = dp[i - 1][0] % MOD if S[i - 1] == '1' or S[i - 1] == '2' else 0
else:
tmpStr = S[i - 1] + S[i]
# print(tmpStr)
dp[i][0] = dp[i - 1][0] % MOD + dp[i - 1][1] % MOD
dp[i][0] %= MOD
if i == 1:
dp[i][1] = 1 if tmpStr in validJoinString else 0
else:
dp[i][1] = dp[i - 2][0] % MOD + dp[i - 2][1] % MOD if tmpStr in validJoinString else 0
dp[i][1] %= MOD
# print(dp)
print((dp[len(S) - 1][0] % MOD + dp[len(S) - 1][1] % MOD)% MOD)
```

사실 지금 바텀업 점화식을 좀 더 명료하게 정리할 수 있지 않을까 싶은데요... 일단 AC를 받았으니 여기까지 하겠습니다.
그리고 맞왜틀이 있었는데요, 항상 모듈로 연산을 잘 해 줍시다.

0 comments on commit 810fe53

Please sign in to comment.