C ++列表中缺少排列
问题陈述
给定任何单词的排列列表。从排列列表中找到丢失的排列。
示例
If permutation is = { “ABC”, “ACB”, “BAC”, “BCA”} then missing
permutations are {“CBA” and “CAB”}算法
创建一组所有给定的字符串
还有所有其他排列的集合
两组之间的收益差
示例
#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