Share 567. permutation in string c++,

..

Một hoán vị của một chuỗi là sự sắp xếp các ký tự của nó theo một thứ tự khác.Ví dụ, các hoán vị của chuỗi "ABC" là "ABC", "ACB", "BAC", "BCA", "Cab" và "CBA".

Trong bài viết này, chúng tôi sẽ thảo luận về cách tìm tất cả các hoán vị của một chuỗi trong C ++.Chúng tôi sẽ sử dụng thuật toán sau:

1. Tạo một vectơ trống để lưu trữ các hoán vị.
2. Tạo đệ quy tất cả các hoán vị của chuỗi, bắt đầu bằng chuỗi trống.
3. Đối với mỗi hoán vị, hãy thêm nó vào vector.
4. Trả lại vectơ của hoán vị.

Sau đây là việc thực hiện thuật toán trong C ++:

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

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

// một hàm để tạo tất cả các hoán vị của chuỗi
vector <String> getperMutations (chuỗi str) {
// Tạo một vectơ trống để lưu trữ các hoán vị
Vector <String> hoán vị;

// tạo ra tất cả các hoán vị của chuỗi, bắt đầu bằng chuỗi trống
tạo ra (str, "", hoán vị);

// Trả lại vectơ của hoán vị
trả lại hoán vị;
}

// Hàm đệ quy để tạo tất cả các hoán vị của chuỗi
void GeneratePermutations (chuỗi str, prefix chuỗi, vector <String> & permutations) {
// Nếu chuỗi trống, hãy thêm tiền tố vào vectơ hoán vị
if (str.length () == 0) {
hoán vị.push_back (tiền tố);
}

// cho mỗi ký tự trong chuỗi
for (int i = 0; i <str.length (); i ++) {
// Tạo tất cả các hoán vị của chuỗi bằng cách xóa ký tự ith
chuỗi Báo chí = str.substr (0, i) + str.substr (i + 1);

// tạo ra tất cả các hoán vị của chuỗi mới
tạo ra (Báo chí, tiền tố + str , hoán vị);
}
}

int main () {
// Nhận chuỗi đầu vào
chuỗi str = "abc";

// Tìm tất cả các hoán vị của chuỗi
vector <String> hoán vị = getpermutations (str);

// In các hoán vị
for (chuỗi hoán vị: hoán vị) {
cout << hoán vị << endl;
}

trả lại 0;
}
`` `

## Ví dụ

Sau đây là một ví dụ về cách sử dụng thuật toán trên để tìm tất cả các hoán vị của chuỗi "ABC":

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

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

int main () {
// Nhận chuỗi đầu vào
chuỗi str = "abc";

// Tìm tất cả các hoán vị của chuỗi
vector <String> hoán vị = getpermutations (str);

// In các hoán vị
for (chuỗi hoán vị: hoán vị) {
cout << hoán vị << endl;
}

trả lại 0;
}
`` `

Đầu ra:

`` `
ABC
ACB
Bac
BCA
taxi
CBA
`` `

## hashtags

* #sợi dây
* #Permuting
* #C ++
* #Programming
* #AlGorithM
=======================================
#String #permutation #C++ #Programming #AlGorithM ## Permutation in String in C++

A permutation of a string is an arrangement of its characters in a different order. For example, the permutations of the string "abc" are "abc", "acb", "bac", "bca", "cab", and "cba".

In this article, we will discuss how to find all permutations of a string in C++. We will use the following algorithm:

1. Create an empty vector to store the permutations.
2. Recursively generate all permutations of the string, starting with the empty string.
3. For each permutation, add it to the vector.
4. Return the vector of permutations.

The following is the implementation of the algorithm in C++:

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

using namespace std;

// A function to generate all permutations of a string
vector<string> getPermutations(string str) {
// Create an empty vector to store the permutations
vector<string> permutations;

// Recursively generate all permutations of the string, starting with the empty string
generatePermutations(str, "", permutations);

// Return the vector of permutations
return permutations;
}

// A recursive function to generate all permutations of a string
void generatePermutations(string str, string prefix, vector<string>& permutations) {
// If the string is empty, add the prefix to the vector of permutations
if (str.length() == 0) {
permutations.push_back(prefix);
}

// For each character in the string
for (int i = 0; i < str.length(); i++) {
// Generate all permutations of the string by removing the ith character
string newStr = str.substr(0, i) + str.substr(i + 1);

// Recursively generate all permutations of the new string
generatePermutations(newStr, prefix + str, permutations);
}
}

int main() {
// Get the input string
string str = "abc";

// Find all permutations of the string
vector<string> permutations = getPermutations(str);

// Print the permutations
for (string permutation : permutations) {
cout << permutation << endl;
}

return 0;
}
```

## Example

The following is an example of how to use the above algorithm to find all permutations of the string "abc":

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

using namespace std;

int main() {
// Get the input string
string str = "abc";

// Find all permutations of the string
vector<string> permutations = getPermutations(str);

// Print the permutations
for (string permutation : permutations) {
cout << permutation << endl;
}

return 0;
}
```

Output:

```
abc
acb
bac
bca
cab
cba
```

## Hashtags

* #String
* #permutation
* #C++
* #Programming
* #AlGorithM
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top