Rearrangements Power Large-Scale Genomic Changes

Point mutations can create changes in populations of organisms from the same species, but they lack the power to create and differentiate entire species. This more arduous work is left to larger mutations called genome rearrangements, which move around huge blocks of DNA. Rearrangements cause major genomic change, and most rearrangements are fatal or seriously damaging to the mutated cell and its descendants (many cancers derive from rearrangements). For this reason, rearrangements that come to influence the genome of an entire species are very rare.
Because rearrangements that affect species evolution occur infrequently, two closely related species will have very similar genomes. Thus, to simplify comparison of two such genomes, researchers first identify similar intervals of DNA from the species, called synteny blocks; over time, rearrangements have created these synteny blocks and heaved them around across the two genomes (often separating blocks onto different chromosomes, see Figure 1.). Enumerating Gene Orders - 图1
Figure 1. Similar regions in mouse and human chromosomes. Image credit: U.S. Department of Energy Human Genome Program
A pair of synteny blocks from two different species are not strictly identical (they are separated by the action of point mutations or very small rearrangements), but for the sake of studying large-scale rearrangements, we consider them to be equivalent. As a result, we can label each synteny block with a positive integer; when comparing two species’ genomes/chromosomes, we then only need to specify the order of its numbered synteny blocks.

Problem

A permutation of length nn is an ordering of the positive integers {1,2,…,n}{1,2,…,n}. For example, π=(5,3,2,1,4)π=(5,3,2,1,4) is a permutation of length 55.
Given: A positive integer n≤7n≤7.
Return: The total number of permutations of length nn, followed by a list of all such permutations (in any order).
Sample Dataset
3
Sample Output
6
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
语雀内容
参考代码:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. class Solution {
  5. public:
  6. void backtrace(int idx, vector<int> &nums, vector<vector<int>> &res) {
  7. // 使用交换法进行枚举全排列
  8. if (idx == nums.size()) res.push_back(nums);
  9. for (int i = idx; i < nums.size(); ++i) {
  10. swap(nums[i], nums[idx]); // 选择 [idx,n) 当当前第一个
  11. backtrace(idx + 1, nums, res);
  12. swap(nums[i], nums[idx]); // 撤销选择
  13. }
  14. }
  15. vector<vector<int>> permute(vector<int>& nums) {
  16. vector<vector<int>> res;
  17. backtrace(0, nums, res);
  18. return res;
  19. }
  20. };
  21. int main() {
  22. int n;
  23. cin >> n;
  24. vector<int> nums(n);
  25. for (int i = 0; i < n; ++i) nums[i] = i + 1;
  26. vector<vector<int>> res = Solution().permute(nums);
  27. cout << res.size() << endl;
  28. for (vector<int> &vec: res) {
  29. for (int num: vec) cout << num << " ";
  30. cout << endl;
  31. }
  32. return 0;
  33. }