day7 1 and 2 done

This commit is contained in:
2024-12-15 12:57:12 +01:00
parent ff0b63dbdd
commit 97cb3b6639
5 changed files with 231 additions and 21 deletions
+29
View File
@@ -3,6 +3,35 @@
#include <iostream>
#include <vector>
// this is defenitely my code
void heapPermutation(std::vector<char> &a, int size,
std::vector<std::vector<char>> &results) {
if (size == 1) {
results.push_back(a); // Store the current permutation
return;
}
for (int i = 0; i < size; i++) {
heapPermutation(a, size - 1, results); // Recur with reduced size
// If size is odd, swap the first element with the last element
if (size % 2 == 1) {
std::swap(a[0], a[size - 1]);
} else { // If size is even, swap the current element with the last element
std::swap(a[i], a[size - 1]);
}
}
}
std::vector<std::vector<char>>
generatePermutations(const std::vector<char> &input) {
std::vector<char> chars = input; // Create a mutable copy of input
std::vector<std::vector<char>> results; // To store all permutations
heapPermutation(chars, chars.size(), results); // Generate permutations
return results; // Return all permutations
}
// end of defenitely my code
int main() {
std::ifstream inputfile("input");
std::string input;