#include <bits/stdc++.h>
using namespace std;
// Helper function to check if a substring is a palindrome
bool is_palindrome(const string &s, int l, int r) {
while (l < r) {
if (s[l] != s[r]) return false;
l++;
r--;
}
return true;
}
int max_palindromic_partition(const string &s) {
int n = s.size();
int chunks = 0;
int i = 0;
while (i < n) {
for (int j = n - 1; j >= i; --j) {
if (is_palindrome(s, i, j)) {
chunks++;
i = j + 1; // Move past the current palindrome
break;
}
}
}
return chunks;
}
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
cout << max_palindromic_partition(s) << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |