From 95f023f12a7fd3d0a6a649a0f1aaa348ebc19b9f Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Thu, 13 Feb 2025 23:08:55 +0800 Subject: [PATCH] feat: 5_longest_palindromic_substring --- 5_longest_palindromic_substring/main.cpp | 59 ++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 5_longest_palindromic_substring/main.cpp diff --git a/5_longest_palindromic_substring/main.cpp b/5_longest_palindromic_substring/main.cpp new file mode 100644 index 0000000..6fa16a3 --- /dev/null +++ b/5_longest_palindromic_substring/main.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; + +class Solution { +public: + int findPalindromeLength(const string &str, int center, int skip) { + for (int radius = skip; center - radius >= 0 && center + radius < str.length(); radius++) { + char r = str[center + radius]; + char l = str[center - radius]; + + if (r != l) { + return radius - 1; + } + } + + return min(center, (int)str.length() - 1 - center); + } + + string longestPalindrome(string s) { + string str = "#"; + for (auto c : s) { + str.push_back(c); + str.push_back('#'); + } + + vector p(str.length(), 0); + int center = 0, right = 0, maxRadius = 0; + for (int index = 1; index < str.length() - 1; index++) { + if (index > right) { + int radius = findPalindromeLength(str, index, 1); + p[index] = radius; + if (radius >= maxRadius) { + maxRadius = radius; + center = index; + right = center + radius; + } + continue; + } + + int indexMirror = center - (index - center); + if (index + p[indexMirror] < right) { + p[index] = p[indexMirror]; + continue; + } + + int radius = findPalindromeLength(str, index, right - index); + p[index] = radius; + if (radius >= maxRadius) { + maxRadius = radius; + center = index; + right = center + radius; + } + } + + int start = (center - maxRadius) / 2; + int length = maxRadius; + return s.substr(start, length); + } +};