49 std::vector<std::vector<int> > cuts(n, std::vector<int>(n, 0));
52 std::vector<std::vector<bool> > is_palindrome(n,
53 std::vector<bool>(n,
false));
56 for (
int i = 0; i < n; i++) {
57 is_palindrome[i][i] =
true;
61 for (
int len = 2; len <= n; len++) {
62 for (
int start_index = 0; start_index < n - len + 1; start_index++) {
63 int end_index = start_index + len - 1;
66 is_palindrome[start_index][end_index] =
67 (str[start_index] == str[end_index]);
69 is_palindrome[start_index][end_index] =
70 (str[start_index] == str[end_index]) &&
71 is_palindrome[start_index + 1][end_index - 1];
74 if (is_palindrome[start_index][end_index]) {
75 cuts[start_index][end_index] = 0;
77 cuts[start_index][end_index] = INT_MAX;
78 for (
int partition = start_index; partition <= end_index - 1;
80 cuts[start_index][end_index] =
81 std::min(cuts[start_index][end_index],
82 cuts[start_index][partition] +
83 cuts[partition + 1][end_index] + 1);
89 return cuts[0][n - 1];
100 std::vector<std::string> custom_input{
"nitik",
"ababbbabbababa",
"abdc"};
103 std::vector<int> calculated_output(3);
105 for (
int i = 0; i < 3; i++) {
106 calculated_output[i] =
107 dynamic_programming::palindrome_partitioning::pal_part(
112 std::vector<int> expected_output{2, 3, 3};
117 for (
int i = 0; i < 3; i++) {
118 assert(expected_output[i] == calculated_output[i]);
121 std::cout <<
"All tests passed successfully!\n";