fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm> // thêm thư viện algorithm
  4. using namespace std;
  5.  
  6. int n, k, a[10];
  7. string s;
  8. vector<string> res;
  9.  
  10. void saveString() {
  11. string r = "";
  12. for (int i = 0; i < k; i++)
  13. r += s[a[i]];
  14. if (find(res.begin(), res.end(), r) == res.end())
  15. res.push_back(r);
  16. }
  17.  
  18. void choose(int i, int start) {
  19. for (int j = start; j < n; j++) {
  20. a[i] = j;
  21. if (i == k - 1)
  22. saveString();
  23. else
  24. choose(i + 1, j + 1);
  25. }
  26. }
  27.  
  28. int main() {
  29. cin >> s;
  30. n = s.length();
  31. for (k = 1; k <= n; k++) {
  32. choose(0, 0);
  33. }
  34. for (int i = 0; i < res.size(); i++)
  35. cout << res[i] << endl;
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 5284KB
stdin
hello
stdout
h
e
l
o
he
hl
ho
el
eo
ll
lo
hel
heo
hll
hlo
ell
elo
llo
hell
helo
hllo
ello
hello