fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. string s;
  5. void backtrack(int d){
  6. if (d==s.size()){
  7. cout<<s<<'\n';
  8. return;
  9. }
  10. for (int i=d;i<s.size();i++){
  11. int ok=1;
  12. for (int j=d;j<i;j++){
  13. if (s[j]==s[i]){
  14. ok=0;
  15. break;
  16. }
  17. }
  18. if (ok==0){
  19. continue;
  20. }
  21. swap(s[d],s[i]);
  22. backtrack(d +1);
  23. swap(s[d],s[i]);
  24. }
  25. }
  26. signed main(){
  27. ios::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29. cout.tie(NULL);
  30. cin>>s;
  31. sort(s.begin(), s.end());
  32. backtrack(0);
  33. }
Success #stdin #stdout 0s 5324KB
stdin
ABA
stdout
AAB
ABA
BAA