fork(1) download
  1. // author: Leonardone @ NEETSDKASU
  2.  
  3. struct Perm {
  4. u: Vec<bool>,
  5. x: Vec<usize>,
  6. init: bool,
  7. }
  8.  
  9. impl Perm {
  10. fn new(size: usize, sel: usize) -> Perm {
  11. Perm {
  12. u: vec![false; size],
  13. x: vec![0; sel],
  14. init: false,
  15. }
  16. }
  17. fn next(&mut self) -> Option<&[usize]> {
  18. if !self.init {
  19. let k = self.u.len() - self.x.len();
  20. for i in 0..self.x.len() {
  21. self.x[i] = i;
  22. self.u[k + i] = false;
  23. self.u[i] = true;
  24. }
  25. self.init = true;
  26. return Some(&self.x);
  27. }
  28. let mut i = self.x.len() - 1;
  29. let mut x = self.x[i];
  30. self.u[x] = false;
  31. loop {
  32. x += 1;
  33. if x >= self.u.len() {
  34. if i == 0 {
  35. self.init = false;
  36. return None;
  37. }
  38. i -= 1;
  39. x = self.x[i];
  40. self.u[x] = false;
  41. continue;
  42. }
  43. if self.u[x] {
  44. continue;
  45. }
  46. self.u[x] = true;
  47. self.x[i] = x;
  48. i += 1;
  49. break;
  50. }
  51. x = 0;
  52. while i < self.x.len() && x < self.u.len() {
  53. if !self.u[x] {
  54. self.u[x] = true;
  55. self.x[i] = x;
  56. i += 1;
  57. }
  58. x += 1;
  59. }
  60. Some(&self.x)
  61. }
  62. }
  63.  
  64. fn main() {
  65. let mut p = Perm::new(5, 3);
  66. while let Some(x) = p.next() {
  67. println!("{:?}", x);
  68. }
  69. }
  70.  
Success #stdin #stdout 0s 4348KB
stdin
Standard input is empty
stdout
[0, 1, 2]
[0, 1, 3]
[0, 1, 4]
[0, 2, 1]
[0, 2, 3]
[0, 2, 4]
[0, 3, 1]
[0, 3, 2]
[0, 3, 4]
[0, 4, 1]
[0, 4, 2]
[0, 4, 3]
[1, 0, 2]
[1, 0, 3]
[1, 0, 4]
[1, 2, 0]
[1, 2, 3]
[1, 2, 4]
[1, 3, 0]
[1, 3, 2]
[1, 3, 4]
[1, 4, 0]
[1, 4, 2]
[1, 4, 3]
[2, 0, 1]
[2, 0, 3]
[2, 0, 4]
[2, 1, 0]
[2, 1, 3]
[2, 1, 4]
[2, 3, 0]
[2, 3, 1]
[2, 3, 4]
[2, 4, 0]
[2, 4, 1]
[2, 4, 3]
[3, 0, 1]
[3, 0, 2]
[3, 0, 4]
[3, 1, 0]
[3, 1, 2]
[3, 1, 4]
[3, 2, 0]
[3, 2, 1]
[3, 2, 4]
[3, 4, 0]
[3, 4, 1]
[3, 4, 2]
[4, 0, 1]
[4, 0, 2]
[4, 0, 3]
[4, 1, 0]
[4, 1, 2]
[4, 1, 3]
[4, 2, 0]
[4, 2, 1]
[4, 2, 3]
[4, 3, 0]
[4, 3, 1]
[4, 3, 2]