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()寻找output形成 的可能组合input,但问题是它不适用于所有input.

谁能告诉我我怎么能找到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 回答