fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void combination(string s, int l, int r) {
  5. if (l == r) {
  6. for (auto g : s) {
  7. cout << g << " ";
  8. }
  9. cout << endl;
  10. return;
  11. }
  12.  
  13. for (int i = l; i <= r; i++) {
  14. swap(s[i], s[l]);
  15. combination(s, l + 1, r);
  16. swap(s[i], s[l]); // backtrack
  17. }
  18. }
  19.  
  20. int main() {
  21. string s;
  22. cin >> s;
  23.  
  24. int l = 0;
  25. int r = s.size() - 1; // last index
  26.  
  27. combination(s, l, r);
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0.01s 5288KB
stdin
abc
stdout
a b c 
a c b 
b a c 
b c a 
c b a 
c a b