1

このようなリストがあります。たとえば、リスト名は次のoutputとおりです。

[[[o, g], [g, o]], [[o, g, o, d]], [[o, d]], [[t, s, n, e, e, e, n, c, s]], [[t, s, n, e, e]], [[e, n, c, s]]]

そして、次のような入力がありますinput

ogodtsneeencs

明らかに、 はinputから形成できますoutput。を形成する可能な組み合わせを見つけるためsubsequences()にを試しましたが、すべての では機能しません。outputinputinput

outputに等しくなる組み合わせを見つける方法を誰か教えてもらえますinputか? そして、おそらくいくつかに保存しますlist

前もって感謝します。

4

1 に答える 1

4

あなたが提供したこの小さなテストデータのセットだけを考えると、私はこれを思いつきました:

def list = [[['o', 'g'], ['g', 'o']], [['o', 'g', 'o', 'd']], [['o', 'd']], [['t', 's', 'n', 'e', 'e', 'e', 'n', 'c', 's']], [['t', 's', 'n', 'e', 'e']], [['e', 'n', 'c', 's']]]

// For every combination of the lists
def result = list.combinations().collect { combination ->
  // Join them into strings
  combination*.join().with { stringcombo ->
    // Then find every string in the list
    stringcombo.findAll { word ->
      // Which is not a substring of another string in the list
      (stringcombo - word).every { it.indexOf( word ) == -1 }
    }
  }.permutations()*.join() // Then get every String permutation of these remaining strings
}.flatten().unique() // and get them into a single unique list

// And print them out
result.each {
  println it
}

どちらが出力されますか:

ogodtsneeencs
tsneeencsogod

より多くのデータがなければ、それが正しいかどうかを判断するのは困難ですが、それはあなたにとって良い出発点になるかもしれません

編集

有効なトークンのすべての順列を返すように更新されました

于 2011-10-19T08:47:03.697 に答える