C++のリストにない順列
問題の説明
任意の単語の順列のリストが与えられます。順列のリストから欠落している順列を見つけます。
例
If permutation is = { “ABC”, “ACB”, “BAC”, “BCA”} then missing
permutations are {“CBA” and “CAB”} アルゴリズム
- 指定されたすべての文字列のセットを作成します
- そしてすべての順列のもう1つのセット
- 2つのセット間の差を返す
例
#include <bits/stdc++.h>
using namespace std;
void findMissingPermutation(string givenPermutation[], size_t
permutationSize) {
vector<string> permutations;
string input = givenPermutation[0];
permutations.push_back(input);
while (true) {
string p = permutations.back();
next_permutation(p.begin(), p.end());
if (p == permutations.front())
break;
permutations.push_back(p);
}
vector<string> missing;
set<string> givenPermutations(givenPermutation,
givenPermutation + permutationSize);
set_difference(permutations.begin(), permutations.end(),
givenPermutations.begin(),
givenPermutations.end(),
back_inserter(missing));
cout << "Missing permutations are" << endl;
for (auto i = missing.begin(); i != missing.end(); ++i)
cout << *i << endl;
}
int main() {
string givenPermutation[] = {"ABC", "ACB", "BAC", "BCA"};
size_t permutationSize = sizeof(givenPermutation) / sizeof(*givenPermutation);
findMissingPermutation(givenPermutation, permutationSize);
return 0;
} 上記のプログラムをコンパイルして実行する場合。次の出力を生成します-
出力
Missing permutations are CAB CBA
-
C++でのリンクリストのフラット化
この問題では、右と下の2つのポインタノードで構成されるリンクリストが表示されます。 右ノード はメインのリンクリストポインタです。 ダウンノード そのノードで始まるセカンダリリンクリスト用です。 リンクリストはすべて並べ替えられています。 私たちのタスクは、リンクリストをフラット化するプログラムを作成することであり、結果のリスト自体がソートされたリストになります。 問題を理解するために例を見てみましょう 入力 出力 1-> 9-> 8 -> 4 -> 6-> 7-> 2-> 3-> 5 ソリューションアプロー
-
C++でのストランドソート
このセクションでは、C++の標準ライブラリを使用して配列またはリンクリストを並べ替える方法を説明します。 C ++には、さまざまな目的に使用できる複数の異なるライブラリがあります。並べ替えもその1つです。 C++関数std::list ::sort()は、リストの要素を昇順で並べ替えます。等しい要素の順序は保持されます。比較のために演算子<を使用します。 例 #include <iostream> #include <list> using namespace std; int main(void) { list<int> l =