Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void recurse(string s, int mask = 0,string out = "")
  7. {
  8. int n = s.length();
  9. if (out.length() == n) cout << ' ' << out<<endl;
  10. for (int i = 0; i < n; i++) {
  11. cout<<"I:"<<i<<"=>";
  12. unsigned bit = 1 << i;
  13. cout<<bit<< " -> " <<mask<<endl;
  14. cout<<"cond:"<<(mask & bit)<<endl;
  15. if (mask & bit) continue;
  16. cout<<out+s[i]<<endl;
  17. recurse(s, mask | bit, out + s[i]);
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. string test = "red";
  24. recurse(test);
  25. cout << endl;
  26. return 0;
  27. }
  28.  
  29. I:0=>1 -> 0
  30. cond:0
  31. r
  32. I:0=>1 -> 1
  33. cond:1
  34. I:1=>2 -> 1
  35. cond:0
  36. re
  37. I:0=>1 -> 3
  38. cond:1
  39. I:1=>2 -> 3
  40. cond:2
  41. I:2=>4 -> 3
  42. cond:0
  43. red
  44. red
  45. I:0=>1 -> 7
  46. cond:1
  47. I:1=>2 -> 7
  48. cond:2
  49. I:2=>4 -> 7 <===== here when bit is 4 and mask is 7 so condition (bit & mask) == true
  50. cond:4 So it will continue .How i = 2 again ? and how mask becomes 1 when Mask will change when it will execute recurse(....)
  51. I:2=>4 -> 1
  52. cond:0
  53. rd
  54. I:0=>1 -> 5
Add Comment
Please, Sign In to add comment