Skip to content

Instantly share code, notes, and snippets.

@kitallis
Created November 7, 2009 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitallis/228734 to your computer and use it in GitHub Desktop.
Save kitallis/228734 to your computer and use it in GitHub Desktop.
###
#
# Let us define a string sequence as :
# S[i] for i > 1 = {
# i = 1 : ()
# i > 1 : (S[1]S[2]S[3]S[4]...S[i-1])
# }
#
# So, S[1] = (), S[2] = (()) , S[3] = (()(())) ...
# In this problem, given I, k and m , you are to find the characters in S[I]
# from index k+1 to k+m.(1 based indices)
#
# Input
# The input consists of several cases. Each case is on a separate line
# containing 3 space separated integers I (1 <; I <; 31) ,
# k (0 <; k < 2I) and m ( 1 <; m <; 10000) . it is
# ensured that k+m is always less than or equal to the length of S[I].
# Input ends with I = k = m = 0.
#
# Output
# For each case,on a separate line, M characters starting from index k+1 in S[i]
# Example
#
# Input:
# 3 0 6
# 3 5 2
# 3 7 1
# 0 0 0
#
# Output:
# (()(()
# ))
# )
#
###
arr = []
while i = gets.split do
break if i == ["0", "0", "0"]
arr << i
end
0.upto(arr.size-1) { |j|
x=(1...h=(arr[j][0].to_i)-1).inject(["()","(())"]) { |a, b|
a.push("(#{a[-2] + a[-1]})")
}
t = x[h]
k,m=arr[j][1].to_i,arr[j][2].to_i
([t]).map {
puts t[k...(k+m)]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment