std::next_permutation
は、このシーケンスの6つの順列を生成する要素[1, 2, 3]
を含む一部のコンテナで使用できます。私がしたいことは、いくつかのセット[1, 2, 3, 4, 5, 6]
がサイズ3のすべての可能な順列を生成することを示しています。この例では、[4, 3, 2]
はこの基準の結果である置換の1つになります。私は自分の組み合わせの関数を記述するのではなく、これを行うSTLの方法(可能な場合)を探しています。私が読んでいなければならない特定のSTL実装C++ STL次の組み合わせによる置換
答えて
これは最も効率的なアルゴリズムではありませんが、簡単です。ソートされた要素から始めなければなりません。次のk-permutationを得るには、最後のn-k個の要素を逆にして、次の順列を取得しようとします。最初のk個の要素は次のk個の置換である。
あなたが+1と言うことは明らかです。 – Barry
現在、(2016年時点で)これを行う単一のSTD機能はありません。あなたが持っている最も近いあなたが望むhttp://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2639.pdf
機能からの提案がnext_partial_permutation
と呼ばれ、(N2639から)のように見えるされている。ここで
template <class BidirectionalIterator >
bool next_partial_permutation(
BidirectionalIterator first ,
BidirectionalIterator middle ,
BidirectionalIterator last)
{
std::reverse(middle , last);
return std::next_permutation(first , last);
}
はSmalltalkの中で書かれたアルゴリズムです。
アルゴリズムの考え方は、長さがm
で、要素が1
とn
の間である配列の辞書順を考慮することです。そのようなarray
が与えられた場合、方法next
は、array
を、その次の部分置換と置き換えます。
私は3つのインスタンス変数
array the current permutation of length m
m the size of array
complement the SortedCollection of integers not in array
で、このクラスで
m: length n: limit
m := length.
array := (1 to: m) asArray.
complement := (m + 1 to: limit) asSortedCollection
を次のように方法next
がarray
それは今なるように修正作品m:n:
インスタンス作成メソッドをクラスを作成しました次の順列を保つ。
アルゴリズムが再帰的ではないことに言及する価値があります。
方法array
IFF nil
とnext
答えは順序(すなわち、array = (n, n-1, ...., n-m+1)
で最後の順列が含まれています。
array = (1 ... m)
で始まり、答えがnil
なるまでnext
を送信するすべての順列を計算するために。
next
| index max h a c |
index := self lastDecreasingIndex.
max := complement max.
h := (index to: m) findLast: [:i | (array at: i) < max] ifAbsent: nil.
h isNil
ifTrue: [
index = 1 ifTrue: [^nil].
a := array at: index - 1.
index to: m do: [:i | complement add: (array at: i)].
c := complement detect: [:cj | a < cj].
array at: index - 1 put: c.
complement remove: c; add: a.
index to: m do: [:i | array at: i put: complement removeFirst]]
ifFalse: [
h := h + index - 1.
a := array at: h.
c := complement detect: [:ci | a < ci].
array at: h put: c.
complement remove: c; add: a.
h + 1 to: m do: [:i | complement add: (array at: i)].
h + 1 to: m do: [:i | array at: i put: complement removeFirst]]
lastDecreasingIndex
| index |
index := m.
[(array at: index - 1) > (array at: index)] whileTrue: [
index := index - 1.
index = 1 ifTrue: [^1]].
^index
- 1. ハスケルの組み合わせと置換
- 2. 2-組み合わせC++
- 3. python - リストとの組み合わせの置換
- 4. Groovy:and、)の組み合わせを置換する
- 5. 複数文字置換による文字列の組み合わせ
- 6. C++/CLRとC++の組み合わせ
- 7. rの連続置換と組み合わせ
- 8. 一覧の更新と置換の組み合わせ
- 9. 置換とcase文の組み合わせ
- 10. CSSリソース - リテラルとランタイム置換の組み合わせ
- 11. Oracleの文字列組み合わせ置換
- 12. 整数の組み合わせをこれらの組み合わせの合計のリストに変換する
- 13. C++の円の組み合わせ
- 14. Python itertoolsの組み合わせの組み合わせ
- 15. C++:デフォルトパラメータの組み合わせ
- 16. C#配列の組み合わせ
- 17. C# - Parallel.ForEachとAsyncの組み合わせ
- 18. すべての組み合わせを文字に置換する
- 19. データウェアハウス:冗長な組み合わせの組み合わせ
- 20. DataFrameの組み合わせ
- 21. 各組み合わせのリストの1つの要素の組み合わせ
- 22. 特別なhtmlタグ(タグの組み合わせ)をnullに置き換える
- 23. ワイアードの組み合わせ
- 24. 配列と組み合わせパターンの組み合わせを見つける
- 25. Pythonの:次第に大きく組み合わせ
- 26. Rは組み合わせ
- 27. 組み合わせ選択
- 28. 異なるコントロールタイプ(チェックボックスとテキストボックス)を組み合わせて同時にC#で組み合わせる方法
- 29. 列の組み合わせの組み合わせデータフレームの行ではない
- 30. クエリの組み合わせを組み合わせるためのLinq構文
http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n –
http://stackoverflow.com/questions/9430568/generating-combinations-in -c –
http://stackoverflow.com/questions/12991758/creating-all-possible-k-combinations-of-n-items-in-c –