Problem 2: Batmanacci
https://open.kattis.com/problems/batmanacci
Given and , find the letter in the Batmanacci string.
We can store the length of each Batmanacci string in a list. (The length of thestring is simply the Fibonacci number.)
Recurrence relation for the lengths:
Length[i] = Length[i - 1] + Length[i - 2]
, with base cases Length[0] = 0
and Length[1] = 1
.
We can build the list of lengths using DP.
Observe that given some string, the first part of this string is the string.
If is within the length of that string, then we can check that string to find the answer. (Denote the length of this string as )
Otherwise, is greater than the length of the string. Then the answer corresponds to the letter in thestring.
We can recursively obtain the desired letter in the string, taking advantage of the word lengths we previously stored.
Base cases in the recursion:
N = 1
: answer is 'N'N = 2
: answer is 'A'
Recurrence relation:
If
K <= Length[N-2]
, go to the case forN-2, K
.Otherwise,
K > Length[N-2]
, so we go to the case forN-1, K-L
.
After these recursive calls, we will arrive at one of the base cases (which gives us the final answer).
Last updated