gets s = gets.chop ans = [-1] * s.size stk = [] s.chars.each.with_index do |c, i| if c == ?( stk << i else ans[stk[-1]] = i ans[i] = stk[-1] stk.pop end end puts ans.map(&:succ)