fork(1) download
  1. #include <climits>
  2. #include <iostream>
  3. #include <stdexcept>
  4. #include <string>
  5. #include <vector>
  6.  
  7. std::vector<std::string> do_split(const std::string& original, const std::vector<bool>& split)
  8. {
  9. if(original.size() != split.size() + 1)
  10. throw std::invalid_argument("Mismatching sizes of string and split");
  11.  
  12. std::vector<std::string> result;
  13. auto beg = original.cbegin();
  14. for(size_t i = 0; i < split.size(); ++i) {
  15. if(split[i]) {
  16. auto end = std::next(original.cbegin(), i + 1);
  17. result.emplace_back(beg, end);
  18. beg = end;
  19. }
  20. }
  21. result.emplace_back(beg, original.cend());
  22. return result;
  23. }
  24.  
  25. int main()
  26. {
  27. using split_t = unsigned;
  28. constexpr size_t limit = sizeof(split_t) * CHAR_BIT;
  29. std::string input;
  30. std::cin >> input;
  31. auto size = input.size();
  32. if(size > limit)
  33. throw std::range_error("Line is too long, maximum: " + std::to_string(limit));
  34. std::cout << "Generating splits:\n";
  35. std::vector<bool> split_v(size - 1);
  36. for(split_t split = 0; split < (1u << (size - 1)); ++split) { //Generating all splits
  37. //Stuffing those to the bool vector for fast extraction
  38. for(size_t i = 0; i < size; ++i)
  39. split_v[i] = (split & 1u << i);
  40. //Extracting current split result
  41. std::vector<std::string> res = do_split(input, split_v);
  42. //Output
  43. for(const auto& s: res)
  44. std::cout << s << ' ';
  45. std::cout << '\n';
  46. }
  47. }
Success #stdin #stdout 0s 3236KB
stdin
Hello
stdout
Generating splits:
Hello 
H ello 
He llo 
H e llo 
Hel lo 
H el lo 
He l lo 
H e l lo 
Hell o 
H ell o 
He ll o 
H e ll o 
Hel l o 
H el l o 
He l l o 
H e l l o