saws #5365(2008/01/16 10:59 GMT) [ Ruby ] Rating0/0=0.00
最適化されてるとはほど遠いですが, 一応Trie構造は作れたと思います. 出力結果: program(ist(i(c(ally)?)|(st)?)?)|(m(a(t(i(c(ally)?)|(st)?)?)|(r)?)|(er)?)?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
Words = %w(program programist programistic programma programmar programmatic programmatically programmatist programmer) class Array def index2(&block) j = nil or each_with_index{|x, i| j = i if yield x} and j end end class Trie def initialize(words) @words = words max = 0 and @words.each{|x| max = x.size if x.size > max} @trie = Array.new(max){Array.new} end def run @words.each{|str| add(str)} substitute puts rep(@trie) end def add(str) (ary = str.split(//)).size.times{|i| x, y = ary[i], (i < ary.size-1 ? ary[i+1] : nil) if (j = @trie[i].index2{|strc| strc[0] == x}) @trie[i][j][1].index2{|char| char == y} ? next : @trie[i][j][1] << y else @trie[i] << [x, [y]] end } end def substitute #文字をインデックスに置換 (@trie.size-1).times{|i| @trie[i].size.times{|j| trie = @trie[i][j][1] trie.map!{|x| @trie[i+1].index2{|y| x == y[0]}}.all?{|x| x} ? trie.sort! : trie.compact! && trie.sort!.unshift(nil) } } end private def rep(ary, i = 0) trie = ary[0][i] if ary.size == 1 trie[0] else if trie[1].size != 1 trie[0] + trie[1].map{|x| if x suffix = (x == trie[1].compact.max ? '?' : '|') "(#{rep(ary[1..-1], x)})#{suffix}" else '' end }.join else trie[0] + (trie[1][0] ? rep(ary[1..-1], trie[1][0]) : '') end end end end trie = Trie.new(Words) trie.run
Rating0/0=0.00-0+
[ reply ]
saws
#5365()
[
Ruby
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]