fork(26) download
  1. #include <algorithm>
  2. #include <cstddef>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. void permutate(const std::string& s, std::vector<int>& index, std::size_t depth, int& count)
  8. {
  9. if (depth == s.size())
  10. {
  11. ++count;
  12. for (std::size_t i = 0; i < s.size(); ++i)
  13. {
  14. std::cout << s[index[i]];
  15. }
  16. std::cout << "\n";
  17. return;
  18. }
  19.  
  20. for (std::size_t i = 0; i < s.size(); ++i)
  21. {
  22. index[depth] = i;
  23. permutate(s, index, depth+1, count);
  24. }
  25. }
  26.  
  27. int main()
  28. {
  29. std::string s("CBA");
  30.  
  31. if (s.find_first_not_of(s.front()) == std::string::npos)
  32. {
  33. std::cout << "Only 1 permutation exists";
  34. return 0;
  35. }
  36.  
  37. std::sort(s.begin(), s.end());
  38.  
  39. std::cout << s << "\n**********\n";
  40.  
  41. std::vector<int> index(s.size());
  42. int count = 0;
  43.  
  44. permutate(s, index, 0, count);
  45.  
  46. std::cout << "\nTotal permutations with repetitions: " << count;
  47. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
ABC
**********
AAA
AAB
AAC
ABA
ABB
ABC
ACA
ACB
ACC
BAA
BAB
BAC
BBA
BBB
BBC
BCA
BCB
BCC
CAA
CAB
CAC
CBA
CBB
CBC
CCA
CCB
CCC

Total permutations with repetitions: 27