Hoán vị không lặp lại C++

Trước tiên, chúng tôi xem xét việc đạt được kết quả mà không cần lặp lại. Bạn có thể truyền một số nguyên n và nó sẽ được chuyển đổi thành chuỗi 1:n hoặc bạn có thể truyền bất kỳ vectơ nào có loại nguyên tử (i. e. logical, integer, numeric, complex, characterraw)

library(RcppAlgos)

## combn output for reference
combn(4, 3)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    2
#> [2,]    2    2    3    3
#> [3,]    3    4    4    4

## This is the same as combn expect the output is transposed
comboGeneral(4, 3)
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    1    2    4
#> [3,]    1    3    4
#> [4,]    2    3    4

## Find all 3-tuple permutations without
## repetition of the numbers c(1, 2, 3, 4).
head(permuteGeneral(4, 3))
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    1    2    4
#> [3,]    1    3    2
#> [4,]    1    3    4
#> [5,]    1    4    2
#> [6,]    1    4    3

## If you don't specify m, the length of v (if v is a vector) or v (if v is a
## scalar (see the examples above)) will be used
v <- c(2, 3, 5, 7, 11, 13)
comboGeneral(v)
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    2    3    5    7   11   13

head(permuteGeneral(v))
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    2    3    5    7   11   13
#> [2,]    2    3    5    7   13   11
#> [3,]    2    3    5   11    7   13
#> [4,]    2    3    5   11   13    7
#> [5,]    2    3    5   13    7   11
#> [6,]    2    3    5   13   11    7


## They are very efficient...
system.time(comboGeneral(25, 12))
#>  user  system elapsed
#> 0.097   0.051   0.148

comboCount(25, 12)
#> [1] 5200300

ht(comboGeneral(25, 12))
#> head -->
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#> [1,]    1    2    3    4    5    6    7    8    9    10    11    12
#> [2,]    1    2    3    4    5    6    7    8    9    10    11    13
#> [3,]    1    2    3    4    5    6    7    8    9    10    11    14
#> [4,]    1    2    3    4    5    6    7    8    9    10    11    15
#> [5,]    1    2    3    4    5    6    7    8    9    10    11    16
#> --------
#> tail -->
#>            [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#> [5200296,]   13   14   15   16   18   19   20   21   22    23    24    25
#> [5200297,]   13   14   15   17   18   19   20   21   22    23    24    25
#> [5200298,]   13   14   16   17   18   19   20   21   22    23    24    25
#> [5200299,]   13   15   16   17   18   19   20   21   22    23    24    25
#> [5200300,]   14   15   16   17   18   19   20   21   22    23    24    25


## And for permutations.. over 8 million instantly
system.time(permuteGeneral(13, 7))
#>    user  system elapsed
#>   0.090   0.050   0.141

permuteCount(13, 7)
#> [1] 8648640

ht(permuteGeneral(13, 7))
#> head -->
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,]    1    2    3    4    5    6    7
#> [2,]    1    2    3    4    5    6    8
#> [3,]    1    2    3    4    5    6    9
#> [4,]    1    2    3    4    5    6   10
#> [5,]    1    2    3    4    5    6   11
#> --------
#> tail -->
#>            [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [8648636,]   13   12   11   10    9    8    3
#> [8648637,]   13   12   11   10    9    8    4
#> [8648638,]   13   12   11   10    9    8    5
#> [8648639,]   13   12   11   10    9    8    6
#> [8648640,]   13   12   11   10    9    8    7


## Factors are preserved
permuteGeneral(factor(c("low", "med", "high"),
               levels = c("low", "med", "high"),
               ordered = TRUE))
#>      [,1] [,2] [,3]
#> [1,] low  med  high
#> [2,] low  high med
#> [3,] med  low  high
#> [4,] med  high low
#> [5,] high low  med
#> [6,] high med  low
#> Levels: low < med < high

Hoán vị còn được gọi là “số sắp xếp” hoặc “thứ tự”, là sự sắp xếp lại các phần tử của danh sách có thứ tự S thành một tương ứng một đối một với chính S. Xâu có độ dài N có N. hoán vị.  

ví dụ

Đầu vào. S = “ABC”
đầu ra. “ABC”, “ACB”, “BAC”, “BCA”, “CBA”, “CAB”

Đầu vào. S = “XY”
đầu ra. “XY”, “YX”

Đề nghị thực hành

Hoán vị của một chuỗi đã cho

Thử nó

In các hoán vị của một chuỗi đã cho bằng cách sử dụng quay lui

Hoán vị không lặp lại C++

Thực hiện theo các bước đã cho để giải quyết vấn đề

  • Tạo hàm permute() với các tham số là chuỗi đầu vào, chỉ số đầu chuỗi, chỉ số kết thúc chuỗi
  • Gọi hàm này với các giá trị chuỗi đầu vào, 0, kích thước của chuỗi – 1
    • Trong hàm này, nếu giá trị của  L và R giống nhau thì in ra cùng một chuỗi
      • Khác chạy vòng lặp for từ L đến R và hoán đổi phần tử hiện tại trong vòng lặp for với inputString[L]
      • Sau đó gọi lại hàm này bằng cách tăng giá trị của L lên 1
      • Sau đó, hoán đổi lại các giá trị đã hoán đổi trước đó để bắt đầu quay lui

Dưới đây là việc thực hiện các phương pháp trên

C++14




// C++ program to print all

// permutations with duplicates allowed

#include

using namespace std;

 

// Function to print permutations of string

// This function takes three parameters:

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
1

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
2

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
4_______1_______5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
6
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
8

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0// C++ program to print all1

// C++ program to print all0// C++ program to print all3 // C++ program to print all4

// C++ program to print all5_______217_______6

// C++ program to print all0// C++ program to print all8

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______218_______1

// C++ program to print all5_______218_______3 // permutations with duplicates allowed4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // permutations with duplicates allowed6

 

// permutations with duplicates allowed7// permutations with duplicates allowed8

// permutations with duplicates allowed7#include 0

 

// permutations with duplicates allowed7#include 2

// permutations with duplicates allowed7#include 4

 

// permutations with duplicates allowed7#include 6

// permutations with duplicates allowed7#include 0

// C++ program to print all5using0

// C++ program to print all0using0

using0

 

using4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 using6

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0using9namespace0namespace1

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 namespace4

 

// C++ program to print all0namespace6

// C++ program to print all0namespace8

// C++ program to print all0std;0 std;1

using0

 

std;3

C




std;4

std;5

std;6

std;7

 

std;8

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 // Function to print permutations of string0// Function to print permutations of string1// Function to print permutations of string2// Function to print permutations of string1// Function to print permutations of string4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0// Function to print permutations of string1 // Function to print permutations of string8

// C++ program to print all0// This function takes three parameters:0

// C++ program to print all0// This function takes three parameters:2

// C++ program to print all0// This function takes three parameters:4

using0

 

// This function takes three parameters:6

// This function takes three parameters:7

// This function takes three parameters:8

// This function takes three parameters:9

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
00

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
02// Function to print permutations of string1
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
04
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
6
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
8

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
12

// C++ program to print all0// C++ program to print all3 // C++ program to print all4

// C++ program to print all5_______1_______17// permutations with duplicates allowed4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
19
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
20

// C++ program to print all0// C++ program to print all8

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______218_______3

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
26

// permutations with duplicates allowed7

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
28

// permutations with duplicates allowed7#include 4

// permutations with duplicates allowed7

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
28#include 6

// C++ program to print all5using0

// C++ program to print all0using0

using0

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
39

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 using6

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0// Function to print permutations of string1

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
45namespace0namespace1

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
50
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
51_______1_______52

// C++ program to print all0namespace8

// C++ program to print all0std;0 std;1

using0

Java




All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
59

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
60

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
62
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
63

 

// C++ program to print all0namespace6

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
70

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______1_______74namespace0namespace1

// C++ program to print all5_______1_______5

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
79

// C++ program to print all5_______1_______81

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
82
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
83

// C++ program to print all5_______1_______85

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
86
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
87
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
88
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// C++ program to print all0using0

 

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
93

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______1_______95

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______1_______97

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______1_______99

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94____217_______01

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______03

// C++ program to print all0// C++ program to print all05

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 // C++ program to print all07
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
6
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
8

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______217_______3 // C++ program to print all4

// permutations with duplicates allowed7// C++ program to print all18

// C++ program to print all5_______217_______8

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// permutations with duplicates allowed7// permutations with duplicates allowed3 // permutations with duplicates allowed4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // permutations with duplicates allowed6

// C++ program to print all27// C++ program to print all28

// C++ program to print all27____217_______30

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
88// C++ program to print all32

// C++ program to print all27// C++ program to print all28

// permutations with duplicates allowed7using0

// C++ program to print all5using0

// C++ program to print all0using0

 

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
93

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______44

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______46

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______48

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______50

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94____217_______52

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______03

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61 // C++ program to print all57
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // C++ program to print all59
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // C++ program to print all61

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______223_______1 // Function to print permutations of string8

// C++ program to print all5_______223_______1// C++ program to print all69

// C++ program to print all5// C++ program to print all71

// C++ program to print all5_______217_______73

// C++ program to print all5_______217_______75

// C++ program to print all5_______222_______0 // C++ program to print all78

// C++ program to print all0using0

using0

 

// C++ program to print all82

Python3




// C++ program to print all83

// C++ program to print all84

 

 

// C++ program to print all85 // C++ program to print all86// C++ program to print all87// C++ program to print all88

// C++ program to print all0std;0 // C++ program to print all91// C++ program to print all87// C++ program to print all93

 

// C++ program to print all94

// C++ program to print all95

// C++ program to print all96

// C++ program to print all97

// C++ program to print all98

 

 

// C++ program to print all85 // permutations with duplicates allowed00

// C++ program to print all0// C++ program to print all3 // permutations with duplicates allowed03// permutations with duplicates allowed04// permutations with duplicates allowed04 // permutations with duplicates allowed06

// C++ program to print all5_______218_______08// permutations with duplicates allowed09

// C++ program to print all0// C++ program to print all8// permutations with duplicates allowed12

// C++ program to print all5_______218_______3 // permutations with duplicates allowed15// permutations with duplicates allowed16 // permutations with duplicates allowed17// permutations with duplicates allowed18

// permutations with duplicates allowed7// permutations with duplicates allowed20// permutations with duplicates allowed04 // permutations with duplicates allowed22

// permutations with duplicates allowed7// permutations with duplicates allowed24____218_______25

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
88// permutations with duplicates allowed27

// permutations with duplicates allowed7// permutations with duplicates allowed20// permutations with duplicates allowed04 // permutations with duplicates allowed31// permutations with duplicates allowed32

 

 

// permutations with duplicates allowed33

// permutations with duplicates allowed34// permutations with duplicates allowed04 namespace0

// permutations with duplicates allowed37_______218_______04 // permutations with duplicates allowed39// permutations with duplicates allowed40

// permutations with duplicates allowed41______218_______04 // permutations with duplicates allowed43// permutations with duplicates allowed40

 

// permutations with duplicates allowed45

// permutations with duplicates allowed46_______1_______86// permutations with duplicates allowed48

 

// permutations with duplicates allowed49

C#




// permutations with duplicates allowed50

// permutations with duplicates allowed51

using // permutations with duplicates allowed53

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
62 // permutations with duplicates allowed55

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
93

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
95

// C++ program to print all0// permutations with duplicates allowed61

// C++ program to print all0// permutations with duplicates allowed63

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
99

// C++ program to print all0// C++ program to print all01

// C++ program to print all0// C++ program to print all03

// C++ program to print all0// C++ program to print all05

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 // C++ program to print all07
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
6
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
8

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______217_______3 // C++ program to print all4

// permutations with duplicates allowed7// permutations with duplicates allowed85

// C++ program to print all5_______217_______8

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// permutations with duplicates allowed7// permutations with duplicates allowed3 // permutations with duplicates allowed4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // permutations with duplicates allowed6

// C++ program to print all27// C++ program to print all28

// C++ program to print all27// permutations with duplicates allowed97

// C++ program to print all27// C++ program to print all28

// permutations with duplicates allowed7using0

// C++ program to print all5using0

// C++ program to print all0using0

 

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
93

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______44

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______46

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______48

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______50

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94____217_______52

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94_______217_______03

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68 // C++ program to print all57
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // C++ program to print all59
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // C++ program to print all61

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______223_______1 // Function to print permutations of string8

// C++ program to print all5_______223_______1#include 35

// C++ program to print all5// C++ program to print all71

// C++ program to print all5_______217_______73

// C++ program to print all5_______217_______75

// C++ program to print all5_______218_______34 #include 44

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
82 // permutations with duplicates allowed34#include 47

// C++ program to print all5_______222_______0 #include 50

// C++ program to print all0using0

 

// C++ program to print all0using4

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 #include 59

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______1_______74namespace0namespace1

// C++ program to print all5_______1_______5 #include 68

// C++ program to print all5namespace8

// C++ program to print all0using0

using0

 

#include 74

PHP




#include 75

#include 76

// permutations with duplicates allowed51

 

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
93

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
95

// permutations with duplicates allowed61

#include 81

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
99

// C++ program to print all01

// C++ program to print all03

#include 85

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
02#include 87#include 88#include 89#include 88#include 91// C++ program to print all93

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0// C++ program to print all3 // permutations with duplicates allowed4#include 89 #include 98#include 91// C++ program to print all93

// C++ program to print all5_______220_______02 #include 87using04using05namespace1

// C++ program to print all0// C++ program to print all8

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______218_______3 // permutations with duplicates allowed4using14 // permutations with duplicates allowed04#include 89namespace1_______220_______14 using19#include 91namespace1using14using23

// C++ program to print all5_______1_______9

// permutations with duplicates allowed7#include 87 using28#include 87#include 88#include 89#include 88using14

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// permutations with duplicates allowed7

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
02#include 87#include 88#include 89 using40#include 91
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// permutations with duplicates allowed7#include 87 using28#include 87#include 88#include 89#include 88using14

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// C++ program to print all5using0

// C++ program to print all0using0

using0

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
93

// C++ program to print all44

// C++ program to print all46

// C++ program to print all48

// C++ program to print all50

// C++ program to print all52

// C++ program to print all03

#include 85 // Function to print permutations of string0using66#include 88using14#include 88using70// C++ program to print all93

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0using74namespace1

// C++ program to print all0using77 // permutations with duplicates allowed04using79// permutations with duplicates allowed4using66

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// C++ program to print all0using74 // permutations with duplicates allowed04using77using87using14using89

// C++ program to print all0using77using87using14using94using77using87using70using98

// C++ program to print all0using77_______220_______87using70using94using74namespace1

// C++ program to print all0std;0 namespace08using77

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

using0

 

using4

#include 87 // permutations with duplicates allowed04namespace0namespace1

namespace17 // permutations with duplicates allowed04

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
51// permutations with duplicates allowed4#include 87
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
02_______219_______87namespace25namespace17 namespace27

 

namespace28

namespace29

Javascript




namespace30

namespace31

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
60

 

#include 85 namespace34

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0// C++ program to print all3 // C++ program to print all4

// permutations with duplicates allowed7namespace40namespace41

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// C++ program to print all5_______217_______8

// C++ program to print all5_______1_______9

// permutations with duplicates allowed7// permutations with duplicates allowed3 namespace49

// permutations with duplicates allowed7

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all27// C++ program to print all28

// C++ program to print all27// permutations with duplicates allowed97

// C++ program to print all27// C++ program to print all28

// permutations with duplicates allowed7using0

// C++ program to print all5using0

using0

 

#include 85 namespace64

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0namespace67

namespace68____221_______69

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

namespace71

// C++ program to print all73

// C++ program to print all75

std;0 namespace75namespace69

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

using0

 

namespace79_______221_______0namespace1

namespace82

namespace83

 

namespace84

namespace85

Đầu ra

ABC
ACB
BAC
BCA
CBA
CAB

Thời gian phức tạp. O(N * N. ) Lưu ý rằng có N. hoán vị và nó yêu cầu thời gian O(N) để in một hoán vị
Không gian phụ trợ. O(R – L)

Ghi chú. Giải pháp trên in các hoán vị trùng lặp nếu có các ký tự lặp lại trong chuỗi đầu vào. Vui lòng xem liên kết bên dưới để biết giải pháp chỉ in các hoán vị riêng biệt ngay cả khi có các bản sao trong đầu vào

  • In tất cả các hoán vị riêng biệt của một chuỗi đã cho với các bản sao.  
  • Hoán vị của một chuỗi đã cho bằng STL

Một cách tiếp cận khác bằng cách nối các chuỗi con.  

Thực hiện theo ý tưởng dưới đây

  • Tạo một hàm đệ quy và chuyển chuỗi đầu vào và một chuỗi lưu trữ hoán vị (ban đầu trống khi được gọi từ hàm chính)
  • Nếu độ dài của chuỗi là 0, hãy in hoán vị
  • Nếu không, hãy chạy một vòng lặp từ i = 0 đến N
    • Coi S[i], là một phần của hoán vị
    • Xóa phần này khỏi chuỗi hiện tại và nối nó vào cuối hoán vị
    • Gọi hàm đệ quy với chuỗi hiện tại không chứa S[i] và hoán vị hiện tại

Dưới đây là việc thực hiện phương pháp này

C++




namespace86

 

#include

using namespace std;

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 namespace92

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0// C++ program to print all3 namespace96

// C++ program to print all5_______221_______98namespace99namespace1

// C++ program to print all5_______222_______0namespace1

// C++ program to print all0using0

// C++ program to print all0// permutations with duplicates allowed3 // permutations with duplicates allowed4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 std;10

// C++ program to print all5_______223_______1 std;13

// C++ program to print all5_______222_______15

// C++ program to print all5_______222_______17

// C++ program to print all5_______222_______19

// C++ program to print all5_______222_______21

// C++ program to print all0using0

using0

 

std;25

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 using6

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all0std;30namespace0namespace1

// C++ program to print all0std;34namespace69namespace1

 

// C++ program to print all0std;38std;39namespace1

// C++ program to print all0std;42

// C++ program to print all0std;0 std;1

using0

Java




std;47 std;48

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
62 // permutations with duplicates allowed55

 

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 std;54

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______217_______3 std;59

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
86std;61

// permutations with duplicates allowed7_______222_______63____221_______99

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// permutations with duplicates allowed7std;0namespace1

// C++ program to print all5using0

 

// C++ program to print all5_______218_______3 // permutations with duplicates allowed4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 std;75
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
86std;77

// permutations with duplicates allowed7// Function to print permutations of string1 std;80

// permutations with duplicates allowed7std;82

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
86std;84

// permutations with duplicates allowed7std;86

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
88
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// permutations with duplicates allowed7std;90

// permutations with duplicates allowed7std;21

// C++ program to print all5using0

// C++ program to print all0using0

 

// C++ program to print all0std;25

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 // Function to print permutations of string03

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______223_______07

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
82 // Function to print permutations of string09

 

// C++ program to print all5_______223_______11____221_______0namespace1

// C++ program to print all5_______223_______15namespace69namespace1

 

// C++ program to print all5_______223_______19std;39

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// C++ program to print all5_______222_______42

// C++ program to print all0using0

using0

 

// Function to print permutations of string27

Python3




// C++ program to print all85 // Function to print permutations of string29

// C++ program to print all0// C++ program to print all3 // permutations with duplicates allowed4// permutations with duplicates allowed39// Function to print permutations of string34// permutations with duplicates allowed04// permutations with duplicates allowed04

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
86// C++ program to print all88

// C++ program to print all5_______218_______08// Function to print permutations of string41// permutations with duplicates allowed04namespace99// C++ program to print all93

// C++ program to print all5std;0

 

// C++ program to print all0// permutations with duplicates allowed3 // permutations with duplicates allowed15// permutations with duplicates allowed16 // permutations with duplicates allowed17// permutations with duplicates allowed4// permutations with duplicates allowed39_______223_______54

// C++ program to print all5_______223_______56____218_______04 // Function to print permutations of string58

// C++ program to print all5_______223_______60// permutations with duplicates allowed04 // Function to print permutations of string62

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
86// Function to print permutations of string64

// C++ program to print all5_______223_______66// permutations with duplicates allowed04 // Function to print permutations of string68// permutations with duplicates allowed25

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
88// Function to print permutations of string71

// C++ program to print all5_______223_______73_______218_______04 // Function to print permutations of string60// permutations with duplicates allowed25 // Function to print permutations of string66

// C++ program to print all5_______223_______79// permutations with duplicates allowed25 // Function to print permutations of string81

 

 

// Function to print permutations of string82

// Function to print permutations of string83_______218_______04 namespace69

 

// Function to print permutations of string86____218_______04 namespace0

 

// permutations with duplicates allowed08____218_______4// Function to print permutations of string91// C++ program to print all93

// Function to print permutations of string93

 

// Function to print permutations of string94

C#




using // permutations with duplicates allowed53

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
62 // permutations with duplicates allowed55

 

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 std;54

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______217_______3 // This function takes three parameters:08

// permutations with duplicates allowed7// This function takes three parameters:10namespace99

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// permutations with duplicates allowed7std;0namespace1

// C++ program to print all5using0

 

// C++ program to print all5_______218_______3 // permutations with duplicates allowed4

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
5 // This function takes three parameters:22

// permutations with duplicates allowed7// Function to print permutations of string1 std;13

// permutations with duplicates allowed7// This function takes three parameters:27

// permutations with duplicates allowed7// This function takes three parameters:29

// permutations with duplicates allowed7std;90

// permutations with duplicates allowed7std;21

// C++ program to print all5using0

// C++ program to print all0using0

 

// C++ program to print all0std;25

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
61
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
68
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
3 // This function takes three parameters:44

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

 

// C++ program to print all5_______223_______11____221_______0namespace1

// C++ program to print all5_______223_______15namespace69namespace1

 

// C++ program to print all5_______224_______56____222_______39

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// C++ program to print all5_______222_______42

// C++ program to print all0using0

using0

 

// This function takes three parameters:64

Javascript




namespace30

// C++ program to print all0// This function takes three parameters:67

// C++ program to print all0// This function takes three parameters:69

#include 85 // This function takes three parameters:71

// This function takes three parameters:72

// C++ program to print all0// C++ program to print all3 // This function takes three parameters:75

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5_______224_______79____221_______99

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
89

// C++ program to print all0using0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94

// C++ program to print all0// permutations with duplicates allowed3// This function takes three parameters:87

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
9

// C++ program to print all5// This function takes three parameters:91

// C++ program to print all5_______224_______93

// C++ program to print all5_______224_______95

// C++ program to print all5_______224_______97

// C++ program to print all5_______222_______21

// C++ program to print all0using0

using0

 

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
004

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
006
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
007

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
009
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
007

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
013
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
014
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
015

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
94

// C++ program to print all0

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
013
All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
019

// C++ program to print all0std;42

 

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  
022

namespace85

Đầu ra

All possible strings are : ABC  ACB  BAC  BCA  CAB  CBA  

Thời gian phức tạp. O(N * N. ) tôi. e. có N. hoán vị và nó yêu cầu thời gian O(N) để in một hoán vị
Không gian phụ trợ. Ô(. S. )

Vui lòng viết nhận xét nếu bạn thấy các mã/thuật toán trên không chính xác hoặc tìm các cách khác để giải quyết vấn đề tương tự