Share next permutation c++

heavycat802

New member
#next hoán vị #c ++ #c ++ lập trình #data Cấu trúc #algorithms

## hoán vị tiếp theo trong C ++ là gì?

Trong khoa học máy tính, một hoán vị là một sự sắp xếp theo thứ tự của một tập hợp các yếu tố.Hán vị tiếp theo của một hoán vị nhất định là hoán vị nhỏ nhất theo nó theo thứ tự từ vựng.Nói cách khác, đó là hoán vị có được bằng cách hoán đổi hai yếu tố đầu tiên không theo thứ tự.

Ví dụ: hoán vị tiếp theo của hoán vị `[1, 2, 3, 4]` là `[1, 2, 4, 3]`.Điều này là do hai yếu tố đầu tiên không có thứ tự là `1` và` 3`, và hoán đổi chúng cho hoán vị `[1, 2, 4, 3].

Hán tiếp theo có thể được tìm thấy bằng cách sử dụng thuật toán sau:

1. Tìm hai yếu tố đầu tiên không có thứ tự.
2. Trao đổi chúng.
3. Đảo ngược thứ tự của tất cả các yếu tố sau hai yếu tố đầu tiên.

Ví dụ, để tìm sự hoán vị tiếp theo của hoán vị `[1, 2, 3, 4], trước tiên chúng ta sẽ tìm thấy hai yếu tố đầu tiên không theo thứ tự, đó là` 1` và `3`.Sau đó, chúng tôi sẽ trao đổi chúng, đưa ra hoán vị `[1, 3, 2, 4]`.Cuối cùng, chúng tôi sẽ đảo ngược thứ tự của tất cả các yếu tố sau hai yếu tố đầu tiên, đưa ra hoán vị `[1, 3, 4, 2]`.

Thuật toán hoán vị tiếp theo có thể được thực hiện trong C ++ như sau:

`` `C ++
#include <Istream>
#include <Thistithm>

sử dụng không gian tên STD;

// một chức năng để tìm hoán vị tiếp theo của một hoán vị nhất định.
Vector <Int> Next_Permuting (Vector <Int> perm) {
// Tìm hai yếu tố đầu tiên không có thứ tự.
int i = perm.size () - 2;
while (i> = 0 && perm > = perm [i + 1]) {
Tôi--;
}

// Nếu không có yếu tố như vậy tồn tại, hoán vị là hoán vị cuối cùng.
if (i <0) {
trả lại perm;
}

// Tìm phần tử nhỏ nhất lớn hơn perm .
int j = perm.size () - 1;
while (j> = 0 && perm [j] <= perm ) {
J--;
}

// hoán đổi perm và perm [j].
hoán đổi (perm , perm [j]);

// đảo ngược thứ tự của tất cả các yếu tố sau perm .
đảo ngược (perm.begin () + i + 1, perm.end ());

trả lại perm;
}

int main () {
// Tạo một vectơ số nguyên.
vector <Int> perm = {1, 2, 3, 4};

// Tìm hoán vị tiếp theo của vector.
Vector <Int> next_perm = next_permuting (perm);

// In hoán vị tiếp theo.
for (int i = 0; i <next_perm.size (); i ++) {
cout << next_perm << "";
}
cout << endl;

trả lại 0;
}
`` `

## hashtags

* #cấu trúc dữ liệu
* #algorithms
* #C ++
* #Programming
* #Nội bộ hoán vị
=======================================
#next Permutation #C++ #C++ Programming #data Structures #algorithms

## What is the next permutation in C++?

In computer science, a permutation is an ordered arrangement of a set of elements. The next permutation of a given permutation is the smallest permutation that follows it in lexicographic order. In other words, it is the permutation that is obtained by swapping the first two elements that are out of order.

For example, the next permutation of the permutation `[1, 2, 3, 4]` is `[1, 2, 4, 3]`. This is because the first two elements that are out of order are `1` and `3`, and swapping them gives the permutation `[1, 2, 4, 3]`.

The next permutation can be found using the following algorithm:

1. Find the first two elements that are out of order.
2. Swap them.
3. Reverse the order of all elements after the first two elements.

For example, to find the next permutation of the permutation `[1, 2, 3, 4]`, we would first find the first two elements that are out of order, which are `1` and `3`. We would then swap them, giving the permutation `[1, 3, 2, 4]`. Finally, we would reverse the order of all elements after the first two elements, giving the permutation `[1, 3, 4, 2]`.

The next permutation algorithm can be implemented in C++ as follows:

```c++
#include <iostream>
#include <algorithm>

using namespace std;

// A function to find the next permutation of a given permutation.
vector<int> next_permutation(vector<int> perm) {
// Find the first two elements that are out of order.
int i = perm.size() - 2;
while (i >= 0 && perm >= perm[i + 1]) {
i--;
}

// If no such elements exist, the permutation is the last permutation.
if (i < 0) {
return perm;
}

// Find the smallest element that is greater than perm.
int j = perm.size() - 1;
while (j >= 0 && perm[j] <= perm) {
j--;
}

// Swap perm and perm[j].
swap(perm, perm[j]);

// Reverse the order of all elements after perm.
reverse(perm.begin() + i + 1, perm.end());

return perm;
}

int main() {
// Create a vector of integers.
vector<int> perm = {1, 2, 3, 4};

// Find the next permutation of the vector.
vector<int> next_perm = next_permutation(perm);

// Print the next permutation.
for (int i = 0; i < next_perm.size(); i++) {
cout << next_perm << " ";
}
cout << endl;

return 0;
}
```

## Hashtags

* #data structures
* #algorithms
* #C++
* #Programming
* #next permutation
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top