a=[] stack = [] gets gets.chomp.chars.each_with_index do |c, i| if c == "(" stack << i else j = stack.pop a[i] = j+1 a[j] = i+1 end end puts a