class Node attr_reader :str, :current def initialize(current, str) @current = current.dup @str = str.dup end end class Yukicoder def initialize s = gets.chomp queue = [] root = Node.new(s, "") queue << root result = Hash.new while !queue.empty? node = queue.shift if node.current.empty? result[node.str] = true else right_node = Node.new(node.current[0..-2], node.str + node.current[-1]) left_node = Node.new(node.current[1..-1], node.str + node.current[0]) queue.push(left_node, right_node) end end puts result.size end end Yukicoder.new