-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome_Partitioning.cpp
More file actions
74 lines (63 loc) · 1.63 KB
/
Palindrome_Partitioning.cpp
File metadata and controls
74 lines (63 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
const int MOD = 1e9 + 7;
const int INF = 1e9;
class Solution {
private:
bool checkpalindrome(string s, int left, int right) {
if (left >= right) return true;
if (s[left] != s[right]) return false;
return checkpalindrome(s, left + 1, right - 1);
}
void divide(string &s, int start, vector<string> ¤t, vector<vector<string>> &result) {
if (start == s.size()) {
result.push_back(current);
return;
}
for (int end = start; end < s.size(); end++) {
if (checkpalindrome(s, start, end)) {
current.push_back(s.substr(start, end - start + 1));
divide(s, end + 1, current, result);
current.pop_back();
}
}
}
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> result;
vector<string> current;
divide(s, 0, current, result);
return result;
}
};
void solve() {
string s;
cin >> s;
Solution sol;
vector<vector<string>> ans = sol.partition(s);
// Print results
for (auto &partition : ans) {
for (auto &str : partition) {
cout << str << " ";
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t; // Uncomment for multiple test cases
while (t--) {
solve();
}
return 0;
}