-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Gold V] Title: Moo 게임, Time: 32 ms, Memory: 31120 KB -BaekjoonHub
- Loading branch information
1 parent
fdff228
commit 3ec6633
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
[점화식 이해] | ||
S(k) = S(k-1) + 'm' + 'o'*(k+2) + S(k-1) # 결과 점화식 | ||
L(k) = 2*L(k-1) + k + 3, L(0) = 3 # 길이 점화식 | ||
[문제 이해] | ||
- 무한히 만들되, N번째 길이까지 만들어졌으면 끝 | ||
[실패] | ||
- (구현x) 재귀를 안돌고 k=0 에서 끝남 | ||
- (메모리 초과) 수열 찾은 뒤 N번째 | ||
[푸는 방법] | ||
- 문자열 규칙을 찾아 바로 계산하기 | ||
- (v) length를 이용해보자! 코치님 코드 참고함 | ||
""" | ||
|
||
s_0 = "moo" | ||
|
||
|
||
def find_length(k): | ||
if k == 0: | ||
return len(s_0) | ||
return 2 * find_length(k - 1) + k + 3 | ||
|
||
|
||
def find_char(n): # idx=N-1 위치의 값을 바로 찾기 | ||
if n < 4: | ||
return s_0[n - 1] | ||
|
||
# n 길이에 맞는 k 먼저 찾기 | ||
k = 0 | ||
cur_length = find_length(k) | ||
while cur_length < n: # 등호 없는 이유; n - 1 | ||
k += 1 | ||
cur_length = find_length(k) | ||
|
||
prev_length = find_length(k - 1) | ||
|
||
if n == prev_length + 1: | ||
return "m" | ||
elif n <= prev_length + 3 + k: | ||
return "o" | ||
else: | ||
return find_char(n - prev_length - 3 - k) | ||
|
||
|
||
N = int(input()) | ||
print(find_char(N)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# [Gold V] Moo 게임 - 5904 | ||
|
||
[문제 링크](https://www.acmicpc.net/problem/5904) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 31120 KB, 시간: 32 ms | ||
|
||
### 분류 | ||
|
||
분할 정복, 재귀 | ||
|
||
### 제출 일자 | ||
|
||
2024년 8월 16일 07:11:09 | ||
|
||
### 문제 설명 | ||
|
||
<p>Moo는 술자리에서 즐겁게 할 수 있는 게임이다. 이 게임은 Moo수열을 각 사람이 하나씩 순서대로 외치면 되는 게임이다.</p> | ||
|
||
<p>Moo 수열은 길이가 무한대이며, 다음과 같이 생겼다. </p> | ||
|
||
<pre>m o o m o o o m o o m o o o o m o o m o o o m o o m o o o o o </pre> | ||
|
||
<p>Moo 수열은 다음과 같은 방법으로 재귀적으로 만들 수 있다. 먼저, S(0)을 길이가 3인 수열 "m o o"이라고 하자. 1보다 크거나 같은 모든 k에 대해서, S(k)는 S(k-1)과 o가 k+2개인 수열 "m o ... o" 와 S(k-1)을 합쳐서 만들 수 있다.</p> | ||
|
||
<pre>S(0) = "m o o" | ||
S(1) = "m o o m o o o m o o" | ||
S(2) = "m o o m o o o m o o m o o o o m o o m o o o m o o"</pre> | ||
|
||
<p>위와 같은 식으로 만들면, 길이가 무한대인 문자열을 만들 수 있으며, 그 수열을 Moo 수열이라고 한다.</p> | ||
|
||
<p>N이 주어졌을 때, Moo 수열의 N번째 글자를 구하는 프로그램을 작성하시오.</p> | ||
|
||
### 입력 | ||
|
||
<p>첫째 줄에 N (1 ≤ N ≤ 10<sup>9</sup>)이 주어진다.</p> | ||
|
||
### 출력 | ||
|
||
<p>N번째 글자를 출력한다.</p> | ||
|