Advertisement
Guest User

Untitled

a guest
May 1st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. // print all combinations of string
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. void combinations(const char str[]);
  7. void combinations(const char str[], char buf[], bool used[], int bufpos, int cur, int len);
  8. void combinations(const char str[], vector<char>& buf, int start, int len);
  9.  
  10. int main(int argc, char* argv[])
  11. {
  12. if(argc!=2)
  13. {
  14. cerr << argv[0] << " [string]" <<endl;
  15. return -1;
  16. }
  17.  
  18. combinations(argv[1]);
  19. return 0;
  20. }
  21.  
  22. void combinations(const char str[], vector<char>& buf, int start, int len)
  23. {
  24. for(int i=start; i<len; ++i)
  25. {
  26. buf.push_back(str[i]);
  27. //cout <<"bufsize: " << buf.size() << endl;
  28. for(size_t j=0; j<buf.size(); ++j)
  29. cout << buf[j];
  30. cout << endl;
  31. if( i < len)
  32. combinations(str, buf, i+1, len);
  33. buf.resize(buf.size()-1);
  34. }
  35. }
  36.  
  37. void combinations(const char str[])
  38. {
  39. int len = strlen(str);
  40. vector<char> buf;
  41. combinations(str, buf, 0, len);
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement