fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool palindrome(string s) {
  5. for (int i = 0; i < s.size() / 2; i++) {
  6. if (s[i] != s[s.size() - 1 - i]) return false;
  7. }
  8. return true;
  9. }
  10.  
  11. vector<vector<string>> res;
  12. vector<string> v;
  13.  
  14. void Try(int start, string s) {
  15. if (start >= s.size()) {
  16. res.push_back(v);
  17. return; // Return here to stop further recursion
  18. }
  19. for (int end = start; end < s.size(); end++) {
  20. if (palindrome(s.substr(start, end - start + 1))) {
  21. v.push_back(s.substr(start, end - start + 1));
  22. Try(end + 1, s);
  23. v.pop_back();
  24. }
  25. }
  26. }
  27.  
  28. int main() {
  29. string s;
  30. cin >> s;
  31. Try(0, s); // Call Try function to generate palindromic partitions
  32. for (auto it : res) {
  33. for (string x : it) {
  34. cout << x << " ";
  35. }
  36. cout << endl;
  37. }
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5284KB
stdin
282882
stdout
2 8 2 8 8 2 
2 8 2 88 2 
2 8 2882 
2 828 8 2 
282 8 8 2 
282 88 2