std::vector<std::vector<int> > cuts(n, std::vector<int>(n, 0));
52std::vector<std::vector<bool> > is_palindrome(n,
53std::vector<bool>(n,
false));
56 for(
inti = 0; i < n; i++) {
57is_palindrome[i][i] =
true;
61 for(
intlen = 2; len <= n; len++) {
62 for(
intstart_index = 0; start_index < n - len + 1; start_index++) {
63 intend_index = start_index + len - 1;
66is_palindrome[start_index][end_index] =
67(str[start_index] == str[end_index]);
69is_palindrome[start_index][end_index] =
70(str[start_index] == str[end_index]) &&
71is_palindrome[start_index + 1][end_index - 1];
74 if(is_palindrome[start_index][end_index]) {
75cuts[start_index][end_index] = 0;
77cuts[start_index][end_index] = INT_MAX;
78 for(
intpartition = start_index; partition <= end_index - 1;
80cuts[start_index][end_index] =
81std::min(cuts[start_index][end_index],
82cuts[start_index][partition] +
83cuts[partition + 1][end_index] + 1);
89 returncuts[0][n - 1];
100std::vector<std::string> custom_input{
"nitik",
"ababbbabbababa",
"abdc"};
103std::vector<int> calculated_output(3);
105 for(
inti = 0; i < 3; i++) {
106calculated_output[i] =
112std::vector<int> expected_output{2, 3, 3};
117 for(
inti = 0; i < 3; i++) {
118assert(expected_output[i] == calculated_output[i]);
121std::cout <<
"All tests passed successfully!\n";
Dynamic Programming algorithms.
Functions for Palindrome Partitioning algorithm.
int pal_part(const std::string &str)
static void test()
Test Function.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4