fork download
  1. for index in 1..9
  2. i=gets.chomp
  3. b= [*
  4. (
  5. i.chars
  6. .group_by{|x| x}
  7. .group_by{|x,y| y.size}
  8. .map{|x|
  9. s=x[1].size
  10. x[1][0...s-s%2].map{|x| x[0]}*''
  11. }*''
  12. ).chars
  13. .each_slice(2)
  14. ]
  15.  
  16. puts [*
  17. [*
  18. b.size
  19. .downto(1)
  20. .map{|n|
  21. b.combination(n)
  22. .map{|t|
  23. [
  24. t*'',
  25. (
  26. h = Hash[t]
  27. s = []
  28. o = 1
  29. i.each_char{|c|
  30. s.push(c) if h.keys.include? c
  31. (
  32. o = false if s.empty? || !h[s.pop].eql?(c)
  33. ) if h.values.include? c
  34. }
  35. o ? s.empty? : o
  36. )
  37. ]
  38. }
  39. }.find{|x| x[0][1]}
  40. ][0]
  41. ][0]
  42. end
Success #stdin #stdout 0.05s 9744KB
stdin
abcc
fffff
aedbedebdcecdec
abcd
aabbcc
abc
abbac
aabcdb
abcdacbd
stdout
ab


abcd
ab
ab

abcd
ab