# Full Permutation & permutation and combination to eliminate the difference of duplicate data

Posted by newbtophp on Sun, 20 Feb 2022 12:24:07 +0100

### Full Permutation & permutation combination

Realize the nuance of excluding duplicates
The permutation and combination here uses an example to expand * * (different combinations of target values)**

The first is full permutation, which is realized in two ways: 1 Swap exchange, 2: array recursion
In essence, they are recursive, but there is still a certain difference in their exclusion of data duplicates.

1, Full arrangement
1.swap exchange method code:

#include<iostream>
using namespace std;
int tmp[3];
void print()//Print data section
{
for (auto v : tmp)
{
cout << v << '\t';
}
cout << endl;
}
bool check(int tmp[], int start, int end)//Exclude duplicates.
{
for (int i = start; i < end; i++)
{
if (tmp[i] == tmp[end])return false;
}
return true;
}
void prim(int tmp[], int start, int end)//Recursive implementation
{
if (start == end - 1) { print(); return; }
for (int i = start; i < end; i++)
{
if (!check(tmp,start,i))continue;
swap(tmp[i], tmp[start]);
prim(tmp, start + 1, end);
swap(tmp[i], tmp[start]);
}
}
int main()
{
for (int i = 0; i < 3; i++)
{
cin >> tmp[i];
}
prim(tmp,0, 3);
return 0;
}

2. Array recursion method:

#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>>kk;
void dfs(vector<int>& num, vector<int>& used, vector<int>& list)
{
if (list.size() == num.size()) { kk.push_back(list); return;}
for (int i = 0; i < num.size(); i++)
{
if (i > 0 && num[i] == num[i - 1] &&!used[i - 1])continue;
//The above if is the key to eliminating duplicates.
if (!used[i])
{
used[i] = 1;
list.push_back(num[i]);
dfs(num, used, list);
list.pop_back();
used[i] = 0;
}
}
}
int main()
{
vector<int>num{ 1,3,5,6 };
vector<int>used(num.size()), list;
dfs(num, used, list);
for (int i = 0; i < kk.size(); i++)
{
for (int j = 0; j < kk[i].size(); j++)
{
cout << kk[i][j] << " ";
}
cout << endl;
}
return 0;
}

We extract the parts of the two code implementations that exclude duplicates
The former: uses a bool function.

bool check(int tmp[], int start, int end)//Exclude duplicates.
{
for (int i = start; i < end; i++)
{
if (tmp[i] == tmp[end])return false;
}
return true;
}

if (!check(tmp,start,i))continue;

The latter: it can be realized directly with one line of code.

if (i > 0 && num[i] == num[i - 1] &&!used[i - 1])continue;

The learning process is bumpy. You can write a simple test data to understand the principle,
The details of eliminating duplicate items in swap implementation are relatively rich. The test data recommended here is 1232
Reason for yourself and see the process.
To feel the true meaning of these two ways!!!

2, The next step is to arrange and combine to eliminate duplicates. The process is different from the above two in some details.

Niuke can do a combination of target values and a series of questions to help tips.
Here we discuss with one of them

#include <iostream>
#include <vector>
#Include < algorithm > / / sort() header file
using namespace std;
vector<int>arr{ 100,10,20,70,60,10,10,50 };
int target = 80;
vector<vector<int>>tmp;
void test(vector<int>& arr, int target, int start, vector<int>& list)
{
if (target <= 0) { if (target == 0)tmp.push_back(list); return; }
for (int i = start; i < arr.size(); i++)
{
if (i > start && arr[i] == arr[i - 1])continue;
list.push_back(arr[i]);
test(arr, target - arr[i], i + 1, list);
list.pop_back();
}
}
int main()
{
vector<int>list;
sort(arr.begin(), arr.end());
test(arr, target, 0, list);
for (int i = 0; i < tmp.size(); i++)
{
for (int j = 0; j < tmp[i].size(); j++)
{
cout << tmp[i][j] << " ";
}
cout << endl;
}
return 0;
}

Extract the code that implements the exclusion of duplicates

if (i > start && arr[i] == arr[i - 1])continue;

Detailed friends can find that this is very similar to the method used in the above recursive implementation of array, but they are still different after careful observation.