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
, character
và raw
]
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 [,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
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
- 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
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 CBA0
All possible strings are : ABC ACB BAC BCA CAB CBA1
All possible strings are : ABC ACB BAC BCA CAB CBA2
All possible strings are : ABC ACB BAC BCA CAB CBA3
All possible strings are : ABC ACB BAC BCA CAB CBA4_______1_______5
All possible strings are : ABC ACB BAC BCA CAB CBA6
All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA8
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0// C++ program to print all
1
// C++ program to print all
0// C++ program to print all
3 // C++ program to print all
4
// C++ program to print all
5_______217_______6
// C++ program to print all
0// C++ program to print all
8
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______218_______1
// C++ program to print all
5_______218_______3 // permutations with duplicates allowed
4
All possible strings are : ABC ACB BAC BCA CAB CBA5
// permutations with duplicates allowed
6
// permutations with duplicates allowed
7// permutations with duplicates allowed
8
// permutations with duplicates allowed
7#include
0
// permutations with duplicates allowed
7#include
2
// permutations with duplicates allowed
7#include
4
// permutations with duplicates allowed
7#include
6
// permutations with duplicates allowed
7#include
0
// C++ program to print all
5using
0
// C++ program to print all
0using
0
using
0
using
4
All possible strings are : ABC ACB BAC BCA CAB CBA5
using
6All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0using
9namespace
0namespace
1
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA5
namespace
4
// C++ program to print all
0namespace
6
// C++ program to print all
0namespace
8
// C++ program to print all
0std;
0 std;
1
using
0
std;
3
C
std;
4
std;
5
std;
6
std;
7
std;
8
All possible strings are : ABC ACB BAC BCA CAB CBA3
// Function to print permutations of string
0// Function to print permutations of string
1// Function to print permutations of string
2// Function to print permutations of string
1// Function to print permutations of string
4All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0// Function to print permutations of string
1 // Function to print permutations of string
8
// C++ program to print all
0// This function takes three parameters:
0
// C++ program to print all
0// This function takes three parameters:
2
// C++ program to print all
0// This function takes three parameters:
4
using
0
// 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 CBA00
All possible strings are : ABC ACB BAC BCA CAB CBA3
All possible strings are : ABC ACB BAC BCA CAB CBA02
// Function to print permutations of string
1All possible strings are : ABC ACB BAC BCA CAB CBA04
All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA6
All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA8
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA12
// C++ program to print all
0// C++ program to print all
3 // C++ program to print all
4
// C++ program to print all
5_______1_______17// permutations with duplicates allowed
4
All possible strings are : ABC ACB BAC BCA CAB CBA19
All possible strings are : ABC ACB BAC BCA CAB CBA20
// C++ program to print all
0// C++ program to print all
8
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______218_______3
All possible strings are : ABC ACB BAC BCA CAB CBA26
// permutations with duplicates allowed
7
All possible strings are : ABC ACB BAC BCA CAB CBA28
// permutations with duplicates allowed
7#include
4
// permutations with duplicates allowed
7
All possible strings are : ABC ACB BAC BCA CAB CBA28
#include
6// C++ program to print all
5using
0
// C++ program to print all
0using
0
using
0
All possible strings are : ABC ACB BAC BCA CAB CBA39
All possible strings are : ABC ACB BAC BCA CAB CBA5
using
6All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0// Function to print permutations of string
1
All possible strings are : ABC ACB BAC BCA CAB CBA45
namespace
0namespace
1// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA50
All possible strings are : ABC ACB BAC BCA CAB CBA51_______1_______52
// C++ program to print all
0namespace
8
// C++ program to print all
0std;
0 std;
1
using
0
Java
All possible strings are : ABC ACB BAC BCA CAB CBA59
All possible strings are : ABC ACB BAC BCA CAB CBA60
All possible strings are : ABC ACB BAC BCA CAB CBA61
All possible strings are : ABC ACB BAC BCA CAB CBA62
All possible strings are : ABC ACB BAC BCA CAB CBA63
// C++ program to print all
0namespace
6
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA61
All possible strings are : ABC ACB BAC BCA CAB CBA68
All possible strings are : ABC ACB BAC BCA CAB CBA3
All possible strings are : ABC ACB BAC BCA CAB CBA70
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______1_______74namespace
0namespace
1
// C++ program to print all
5_______1_______5
All possible strings are : ABC ACB BAC BCA CAB CBA79
// C++ program to print all
5_______1_______81
All possible strings are : ABC ACB BAC BCA CAB CBA82
All possible strings are : ABC ACB BAC BCA CAB CBA83
// C++ program to print all
5_______1_______85
All possible strings are : ABC ACB BAC BCA CAB CBA86
All possible strings are : ABC ACB BAC BCA CAB CBA87
All possible strings are : ABC ACB BAC BCA CAB CBA88
All possible strings are : ABC ACB BAC BCA CAB CBA89
// C++ program to print all
0using
0
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA93
All possible strings are : ABC ACB BAC BCA CAB CBA94_______1_______95
All possible strings are : ABC ACB BAC BCA CAB CBA94_______1_______97
All possible strings are : ABC ACB BAC BCA CAB CBA94_______1_______99
All possible strings are : ABC ACB BAC BCA CAB CBA94____217_______01
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______03
// C++ program to print all
0// C++ program to print all
05
All possible strings are : ABC ACB BAC BCA CAB CBA3
// C++ program to print all
07All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA6
All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA8
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______217_______3 // C++ program to print all
4
// permutations with duplicates allowed
7// C++ program to print all
18
// C++ program to print all
5_______217_______8
All possible strings are : ABC ACB BAC BCA CAB CBA9
// permutations with duplicates allowed
7// permutations with duplicates allowed
3 // permutations with duplicates allowed
4
All possible strings are : ABC ACB BAC BCA CAB CBA5
// permutations with duplicates allowed
6// C++ program to print all
27// C++ program to print all
28
// C++ program to print all
27____217_______30
All possible strings are : ABC ACB BAC BCA CAB CBA88
// C++ program to print all
32// C++ program to print all
27// C++ program to print all
28
// permutations with duplicates allowed
7using
0
// C++ program to print all
5using
0
// C++ program to print all
0using
0
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA93
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______44
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______46
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______48
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______50
All possible strings are : ABC ACB BAC BCA CAB CBA94____217_______52
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______03
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA61
// C++ program to print all
57All possible strings are : ABC ACB BAC BCA CAB CBA5
// C++ program to print all
59All possible strings are : ABC ACB BAC BCA CAB CBA5
// C++ program to print all
61// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______223_______1 // Function to print permutations of string
8
// C++ program to print all
5_______223_______1// C++ program to print all
69
// C++ program to print all
5// C++ program to print all
71
// C++ program to print all
5_______217_______73
// C++ program to print all
5_______217_______75
// C++ program to print all
5_______222_______0 // C++ program to print all
78
// C++ program to print all
0using
0
using
0
// C++ program to print all
82
Python3
// C++ program to print all
83
// C++ program to print all
84
// C++ program to print all
85 // C++ program to print all
86// C++ program to print all
87// C++ program to print all
88
// C++ program to print all
0std;
0 // C++ program to print all
91// C++ program to print all
87// C++ program to print all
93
// C++ program to print all
94
// C++ program to print all
95
// C++ program to print all
96
// C++ program to print all
97
// C++ program to print all
98
// C++ program to print all
85 // permutations with duplicates allowed
00
// C++ program to print all
0// C++ program to print all
3 // permutations with duplicates allowed
03// permutations with duplicates allowed
04// permutations with duplicates allowed
04 // permutations with duplicates allowed
06
// C++ program to print all
5_______218_______08// permutations with duplicates allowed
09
// C++ program to print all
0// C++ program to print all
8// permutations with duplicates allowed
12
// C++ program to print all
5_______218_______3 // permutations with duplicates allowed
15// permutations with duplicates allowed
16 // permutations with duplicates allowed
17// permutations with duplicates allowed
18
// permutations with duplicates allowed
7// permutations with duplicates allowed
20// permutations with duplicates allowed
04 // permutations with duplicates allowed
22
// permutations with duplicates allowed
7// permutations with duplicates allowed
24____218_______25
All possible strings are : ABC ACB BAC BCA CAB CBA88
// permutations with duplicates allowed
27// permutations with duplicates allowed
7// permutations with duplicates allowed
20// permutations with duplicates allowed
04 // permutations with duplicates allowed
31// permutations with duplicates allowed
32
// permutations with duplicates allowed
33
// permutations with duplicates allowed
34// permutations with duplicates allowed
04 namespace
0
// permutations with duplicates allowed
37_______218_______04 // permutations with duplicates allowed
39// permutations with duplicates allowed
40
// permutations with duplicates allowed
41______218_______04 // permutations with duplicates allowed
43// permutations with duplicates allowed
40
// permutations with duplicates allowed
45
// permutations with duplicates allowed
46_______1_______86// permutations with duplicates allowed
48
// permutations with duplicates allowed
49
C#
// permutations with duplicates allowed
50
// permutations with duplicates allowed
51
using
// permutations with duplicates allowed
53
All possible strings are : ABC ACB BAC BCA CAB CBA62
// permutations with duplicates allowed
55// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA93
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA95
// C++ program to print all
0// permutations with duplicates allowed
61
// C++ program to print all
0// permutations with duplicates allowed
63
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA99
// C++ program to print all
0// C++ program to print all
01
// C++ program to print all
0// C++ program to print all
03
// C++ program to print all
0// C++ program to print all
05
All possible strings are : ABC ACB BAC BCA CAB CBA68
All possible strings are : ABC ACB BAC BCA CAB CBA3
// C++ program to print all
07All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA6
All possible strings are : ABC ACB BAC BCA CAB CBA5
All possible strings are : ABC ACB BAC BCA CAB CBA8
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______217_______3 // C++ program to print all
4
// permutations with duplicates allowed
7// permutations with duplicates allowed
85
// C++ program to print all
5_______217_______8
All possible strings are : ABC ACB BAC BCA CAB CBA9
// permutations with duplicates allowed
7// permutations with duplicates allowed
3 // permutations with duplicates allowed
4
All possible strings are : ABC ACB BAC BCA CAB CBA5
// permutations with duplicates allowed
6// C++ program to print all
27// C++ program to print all
28
// C++ program to print all
27// permutations with duplicates allowed
97
// C++ program to print all
27// C++ program to print all
28
// permutations with duplicates allowed
7using
0
// C++ program to print all
5using
0
// C++ program to print all
0using
0
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA93
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______44
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______46
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______48
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______50
All possible strings are : ABC ACB BAC BCA CAB CBA94____217_______52
All possible strings are : ABC ACB BAC BCA CAB CBA94_______217_______03
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA61
All possible strings are : ABC ACB BAC BCA CAB CBA68
// C++ program to print all
57All possible strings are : ABC ACB BAC BCA CAB CBA5
// C++ program to print all
59All possible strings are : ABC ACB BAC BCA CAB CBA5
// C++ program to print all
61// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______223_______1 // Function to print permutations of string
8
// C++ program to print all
5_______223_______1#include
35
// C++ program to print all
5// C++ program to print all
71
// C++ program to print all
5_______217_______73
// C++ program to print all
5_______217_______75
// C++ program to print all
5_______218_______34 #include
44
All possible strings are : ABC ACB BAC BCA CAB CBA82
// permutations with duplicates allowed
34#include
47// C++ program to print all
5_______222_______0 #include
50
// C++ program to print all
0using
0
// C++ program to print all
0using
4
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA61
All possible strings are : ABC ACB BAC BCA CAB CBA68
All possible strings are : ABC ACB BAC BCA CAB CBA3
#include
59// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______1_______74namespace
0namespace
1
// C++ program to print all
5_______1_______5 #include
68
// C++ program to print all
5namespace
8
// C++ program to print all
0using
0
using
0
#include
74
PHP
#include
75
#include
76
// permutations with duplicates allowed
51
All possible strings are : ABC ACB BAC BCA CAB CBA93
All possible strings are : ABC ACB BAC BCA CAB CBA95
// permutations with duplicates allowed
61
#include
81
All possible strings are : ABC ACB BAC BCA CAB CBA99
// C++ program to print all
01
// C++ program to print all
03
#include
85
All possible strings are : ABC ACB BAC BCA CAB CBA02
#include
87#include
88#include
89#include
88#include
91// C++ program to print all
93All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0// C++ program to print all
3 // permutations with duplicates allowed
4#include
89 #include
98#include
91// C++ program to print all
93
// C++ program to print all
5_______220_______02 #include
87using
04using
05namespace
1
// C++ program to print all
0// C++ program to print all
8
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______218_______3 // permutations with duplicates allowed
4using
14 // permutations with duplicates allowed
04#include
89namespace
1_______220_______14 using
19#include
91namespace
1using
14using
23
// C++ program to print all
5_______1_______9
// permutations with duplicates allowed
7#include
87 using
28#include
87#include
88#include
89#include
88using
14
All possible strings are : ABC ACB BAC BCA CAB CBA89
// permutations with duplicates allowed
7
All possible strings are : ABC ACB BAC BCA CAB CBA02
#include
87#include
88#include
89 using
40#include
91All possible strings are : ABC ACB BAC BCA CAB CBA89
// permutations with duplicates allowed
7#include
87 using
28#include
87#include
88#include
89#include
88using
14
All possible strings are : ABC ACB BAC BCA CAB CBA89
// C++ program to print all
5using
0
// C++ program to print all
0using
0
using
0
All possible strings are : ABC ACB BAC BCA CAB CBA93
// C++ program to print all
44
// C++ program to print all
46
// C++ program to print all
48
// C++ program to print all
50
// C++ program to print all
52
// C++ program to print all
03
#include
85 // Function to print permutations of string
0using
66#include
88using
14#include
88using
70// C++ program to print all
93
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0using
74namespace
1
// C++ program to print all
0using
77 // permutations with duplicates allowed
04using
79// permutations with duplicates allowed
4using
66
All possible strings are : ABC ACB BAC BCA CAB CBA89
// C++ program to print all
0using
74 // permutations with duplicates allowed
04using
77using
87using
14using
89
// C++ program to print all
0using
77using
87using
14using
94using
77using
87using
70using
98
// C++ program to print all
0using
77_______220_______87using
70using
94using
74namespace
1
// C++ program to print all
0std;
0 namespace
08using
77
All possible strings are : ABC ACB BAC BCA CAB CBA89
using
0
using
4
#include
87 // permutations with duplicates allowed
04namespace
0namespace
1
namespace
17 // permutations with duplicates allowed
04
All possible strings are : ABC ACB BAC BCA CAB CBA51
// permutations with duplicates allowed
4#include
87All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA02_______219_______87
namespace
25namespace
17 namespace
27
namespace
28
namespace
29
Javascript
namespace
30
namespace
31
All possible strings are : ABC ACB BAC BCA CAB CBA60
#include
85 namespace
34
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0// C++ program to print all
3 // C++ program to print all
4
// permutations with duplicates allowed
7namespace
40namespace
41
All possible strings are : ABC ACB BAC BCA CAB CBA89
// C++ program to print all
5_______217_______8
// C++ program to print all
5_______1_______9
// permutations with duplicates allowed
7// permutations with duplicates allowed
3 namespace
49
// permutations with duplicates allowed
7
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
27// C++ program to print all
28
// C++ program to print all
27// permutations with duplicates allowed
97
// C++ program to print all
27// C++ program to print all
28
// permutations with duplicates allowed
7using
0
// C++ program to print all
5using
0
using
0
#include
85 namespace
64
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0namespace
67
namespace
68____221_______69
All possible strings are : ABC ACB BAC BCA CAB CBA89
namespace
71
// C++ program to print all
73
// C++ program to print all
75
std;
0 namespace
75namespace
69
All possible strings are : ABC ACB BAC BCA CAB CBA89
using
0
namespace
79_______221_______0namespace
1
namespace
82
namespace
83
namespace
84
namespace
85
Đầ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++
namespace
86
#include
using
namespace
std;
All possible strings are : ABC ACB BAC BCA CAB CBA3
namespace
92All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0// C++ program to print all
3 namespace
96
// C++ program to print all
5_______221_______98namespace
99namespace
1
// C++ program to print all
5_______222_______0namespace
1
// C++ program to print all
0using
0
// C++ program to print all
0// permutations with duplicates allowed
3 // permutations with duplicates allowed
4
All possible strings are : ABC ACB BAC BCA CAB CBA5
std;
10// C++ program to print all
5_______223_______1 std;
13
// C++ program to print all
5_______222_______15
// C++ program to print all
5_______222_______17
// C++ program to print all
5_______222_______19
// C++ program to print all
5_______222_______21
// C++ program to print all
0using
0
using
0
std;
25
All possible strings are : ABC ACB BAC BCA CAB CBA5
using
6All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
0std;
30namespace
0namespace
1
// C++ program to print all
0std;
34namespace
69namespace
1
// C++ program to print all
0std;
38std;
39namespace
1
// C++ program to print all
0std;
42
// C++ program to print all
0std;
0 std;
1
using
0
Java
std;
47 std;
48
All possible strings are : ABC ACB BAC BCA CAB CBA62
// permutations with duplicates allowed
55
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA68
All possible strings are : ABC ACB BAC BCA CAB CBA3
std;
54// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______217_______3 std;
59
All possible strings are : ABC ACB BAC BCA CAB CBA86
std;
61// permutations with duplicates allowed
7_______222_______63____221_______99
All possible strings are : ABC ACB BAC BCA CAB CBA89
// permutations with duplicates allowed
7std;
0namespace
1
// C++ program to print all
5using
0
// C++ program to print all
5_______218_______3 // permutations with duplicates allowed
4
All possible strings are : ABC ACB BAC BCA CAB CBA5
std;
75All possible strings are : ABC ACB BAC BCA CAB CBA86
std;
77// permutations with duplicates allowed
7// Function to print permutations of string
1 std;
80
// permutations with duplicates allowed
7std;
82
All possible strings are : ABC ACB BAC BCA CAB CBA86
std;
84// permutations with duplicates allowed
7std;
86
All possible strings are : ABC ACB BAC BCA CAB CBA88
All possible strings are : ABC ACB BAC BCA CAB CBA89
// permutations with duplicates allowed
7std;
90
// permutations with duplicates allowed
7std;
21
// C++ program to print all
5using
0
// C++ program to print all
0using
0
// C++ program to print all
0std;
25
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA61
All possible strings are : ABC ACB BAC BCA CAB CBA68
All possible strings are : ABC ACB BAC BCA CAB CBA3
// Function to print permutations of string
03// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______223_______07
All possible strings are : ABC ACB BAC BCA CAB CBA82
// Function to print permutations of string
09
// C++ program to print all
5_______223_______11____221_______0namespace
1
// C++ program to print all
5_______223_______15namespace
69namespace
1
// C++ program to print all
5_______223_______19std;
39
All possible strings are : ABC ACB BAC BCA CAB CBA89
// C++ program to print all
5_______222_______42
// C++ program to print all
0using
0
using
0
// Function to print permutations of string
27
Python3
// C++ program to print all
85 // Function to print permutations of string
29
// C++ program to print all
0// C++ program to print all
3 // permutations with duplicates allowed
4// permutations with duplicates allowed
39// Function to print permutations of string
34// permutations with duplicates allowed
04// permutations with duplicates allowed
04
All possible strings are : ABC ACB BAC BCA CAB CBA86
// C++ program to print all
88// C++ program to print all
5_______218_______08// Function to print permutations of string
41// permutations with duplicates allowed
04namespace
99// C++ program to print all
93
// C++ program to print all
5std;
0
// C++ program to print all
0// permutations with duplicates allowed
3 // permutations with duplicates allowed
15// permutations with duplicates allowed
16 // permutations with duplicates allowed
17// permutations with duplicates allowed
4// permutations with duplicates allowed
39_______223_______54
// C++ program to print all
5_______223_______56____218_______04 // Function to print permutations of string
58
// C++ program to print all
5_______223_______60// permutations with duplicates allowed
04 // Function to print permutations of string
62
All possible strings are : ABC ACB BAC BCA CAB CBA86
// Function to print permutations of string
64// C++ program to print all
5_______223_______66// permutations with duplicates allowed
04 // Function to print permutations of string
68// permutations with duplicates allowed
25
All possible strings are : ABC ACB BAC BCA CAB CBA88
// Function to print permutations of string
71// C++ program to print all
5_______223_______73_______218_______04 // Function to print permutations of string
60// permutations with duplicates allowed
25 // Function to print permutations of string
66
// C++ program to print all
5_______223_______79// permutations with duplicates allowed
25 // Function to print permutations of string
81
// Function to print permutations of string
82
// Function to print permutations of string
83_______218_______04 namespace
69
// Function to print permutations of string
86____218_______04 namespace
0
// permutations with duplicates allowed
08____218_______4// Function to print permutations of string
91// C++ program to print all
93
// Function to print permutations of string
93
// Function to print permutations of string
94
C#
using
// permutations with duplicates allowed
53
All possible strings are : ABC ACB BAC BCA CAB CBA61
All possible strings are : ABC ACB BAC BCA CAB CBA62
// permutations with duplicates allowed
55
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA68
All possible strings are : ABC ACB BAC BCA CAB CBA3
std;
54// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______217_______3 // This function takes three parameters:
08
// permutations with duplicates allowed
7// This function takes three parameters:
10namespace
99
All possible strings are : ABC ACB BAC BCA CAB CBA89
// permutations with duplicates allowed
7std;
0namespace
1
// C++ program to print all
5using
0
// C++ program to print all
5_______218_______3 // permutations with duplicates allowed
4
All possible strings are : ABC ACB BAC BCA CAB CBA5
// This function takes three parameters:
22// permutations with duplicates allowed
7// Function to print permutations of string
1 std;
13
// permutations with duplicates allowed
7// This function takes three parameters:
27
// permutations with duplicates allowed
7// This function takes three parameters:
29
// permutations with duplicates allowed
7std;
90
// permutations with duplicates allowed
7std;
21
// C++ program to print all
5using
0
// C++ program to print all
0using
0
// C++ program to print all
0std;
25
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA61
All possible strings are : ABC ACB BAC BCA CAB CBA68
All possible strings are : ABC ACB BAC BCA CAB CBA3
// This function takes three parameters:
44// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______223_______11____221_______0namespace
1
// C++ program to print all
5_______223_______15namespace
69namespace
1
// C++ program to print all
5_______224_______56____222_______39
All possible strings are : ABC ACB BAC BCA CAB CBA89
// C++ program to print all
5_______222_______42
// C++ program to print all
0using
0
using
0
// This function takes three parameters:
64
Javascript
namespace
30
// C++ program to print all
0// This function takes three parameters:
67
// C++ program to print all
0// This function takes three parameters:
69
#include
85 // This function takes three parameters:
71
// This function takes three parameters:
72
// C++ program to print all
0// C++ program to print all
3 // This function takes three parameters:
75
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5_______224_______79____221_______99
All possible strings are : ABC ACB BAC BCA CAB CBA89
// C++ program to print all
0using
0
All possible strings are : ABC ACB BAC BCA CAB CBA94
// C++ program to print all
0// permutations with duplicates allowed
3// This function takes three parameters:
87
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA9
// C++ program to print all
5// This function takes three parameters:
91
// C++ program to print all
5_______224_______93
// C++ program to print all
5_______224_______95
// C++ program to print all
5_______224_______97
// C++ program to print all
5_______222_______21
// C++ program to print all
0using
0
using
0
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA004
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA006
All possible strings are : ABC ACB BAC BCA CAB CBA007
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA009
All possible strings are : ABC ACB BAC BCA CAB CBA007
All possible strings are : ABC ACB BAC BCA CAB CBA94
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA013
All possible strings are : ABC ACB BAC BCA CAB CBA014
All possible strings are : ABC ACB BAC BCA CAB CBA015
All possible strings are : ABC ACB BAC BCA CAB CBA94
// C++ program to print all
0
All possible strings are : ABC ACB BAC BCA CAB CBA013
All possible strings are : ABC ACB BAC BCA CAB CBA019
// C++ program to print all
0std;
42
All possible strings are : ABC ACB BAC BCA CAB CBA022
namespace
85
Đầ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ự