classSolution { public: string longestPalindrome(string s){ int n = s.size(); vector<vector<bool>> is_palindrome(n, vector<bool>(n, false));
// a single character must be a palindrome for (int i = 0; i < n; ++i) { is_palindrome[i][i] = true; } int max_palindrome_size = 1; string max_palindrome = s.substr(0, 1);
// verify any continious two characters are a palindrome for (int i = 0; i < n - 1; ++i) { if (s[i] == s[i + 1]) { is_palindrome[i][i + 1] = true;
// verify any continious string whose length is larger than or equal to 3. // its state is related to 2 factors - its boundary characters, and its substring without the boundary characters for (int sub_size = 3; sub_size <= n; ++sub_size) { for (int begin_index = 0; begin_index <= n - sub_size; ++begin_index) { int end_index = begin_index + sub_size - 1; if (s[begin_index] == s[end_index] && is_palindrome[begin_index + 1][end_index - 1]) { is_palindrome[begin_index][end_index] = true;