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 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ụ
Đề 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 luiThực hiện theo các bước đã cho để giải quyết vấn đề
Dưới đây là việc thực hiện các phương pháp trên C++14
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA5 // permutations with duplicates allowed 6
All possible strings are : ABC ACB BAC BCA CAB CBA5 using 6All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA5 namespace 4
C
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
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
All possible strings are : ABC ACB BAC BCA CAB CBA5 All possible strings are : ABC ACB BAC BCA CAB CBA12
All possible strings are : ABC ACB BAC BCA CAB CBA19 All possible strings are : ABC ACB BAC BCA CAB CBA20
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA26
All possible strings are : ABC ACB BAC BCA CAB CBA28
All possible strings are : ABC ACB BAC BCA CAB CBA28 #include 6
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
All possible strings are : ABC ACB BAC BCA CAB CBA45 namespace 0namespace 1
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
JavaAll 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
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA79
All possible strings are : ABC ACB BAC BCA CAB CBA82 All possible strings are : ABC ACB BAC BCA CAB CBA83
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
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
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA5 // permutations with duplicates allowed 6
All possible strings are : ABC ACB BAC BCA CAB CBA88 // C++ program to print all 32
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
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
Python3
All possible strings are : ABC ACB BAC BCA CAB CBA88 // permutations with duplicates allowed 27
C#
All possible strings are : ABC ACB BAC BCA CAB CBA62 // permutations with duplicates allowed 55
All possible strings are : ABC ACB BAC BCA CAB CBA93
All possible strings are : ABC ACB BAC BCA CAB CBA95
All possible strings are : ABC ACB BAC BCA CAB CBA99
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA5 // permutations with duplicates allowed 6
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
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA82 // permutations with duplicates allowed 34#include 47
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
PHP
All possible strings are : ABC ACB BAC BCA CAB CBA93 All possible strings are : ABC ACB BAC BCA CAB CBA95
All possible strings are : ABC ACB BAC BCA CAB CBA99
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA89
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
All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA93
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA89
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
Javascript
All possible strings are : ABC ACB BAC BCA CAB CBA60
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA89
Đầ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ị 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
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
Dưới đây là việc thực hiện phương pháp này C++
All possible strings are : ABC ACB BAC BCA CAB CBA3 namespace 92All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA5 std; 10
All possible strings are : ABC ACB BAC BCA CAB CBA5 using 6All possible strings are : ABC ACB BAC BCA CAB CBA9
Java
All possible strings are : ABC ACB BAC BCA CAB CBA62 // permutations with duplicates allowed 55
All possible strings are : ABC ACB BAC BCA CAB CBA68 All possible strings are : ABC ACB BAC BCA CAB CBA3 std; 54
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA86 std; 61
All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA5 std; 75All possible strings are : ABC ACB BAC BCA CAB CBA86 std; 77
All possible strings are : ABC ACB BAC BCA CAB CBA86 std; 84
All possible strings are : ABC ACB BAC BCA CAB CBA88 All possible strings are : ABC ACB BAC BCA CAB CBA89
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA82 // Function to print permutations of string 09
All possible strings are : ABC ACB BAC BCA CAB CBA89
Python3
All possible strings are : ABC ACB BAC BCA CAB CBA86 // C++ program to print all 88
All possible strings are : ABC ACB BAC BCA CAB CBA86 // Function to print permutations of string 64
All possible strings are : ABC ACB BAC BCA CAB CBA88 // Function to print permutations of string 71
C#
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
All possible strings are : ABC ACB BAC BCA CAB CBA68 All possible strings are : ABC ACB BAC BCA CAB CBA3 std; 54
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA5 // This function takes three parameters: 22
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
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA89
Javascript
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA89
All possible strings are : ABC ACB BAC BCA CAB CBA94
All possible strings are : ABC ACB BAC BCA CAB CBA9
All possible strings are : ABC ACB BAC BCA CAB CBA004
All possible strings are : ABC ACB BAC BCA CAB CBA006 All possible strings are : ABC ACB BAC BCA CAB CBA007
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
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
All possible strings are : ABC ACB BAC BCA CAB CBA013 All possible strings are : ABC ACB BAC BCA CAB CBA019
All possible strings are : ABC ACB BAC BCA CAB CBA022
Đầ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ị 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ự |