This action will force synchronization from 编程语言算法集/Python, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
def palindromic_string(input_string: str) -> str:""">>> palindromic_string('abbbaba')'abbba'>>> palindromic_string('ababa')'ababa'Manacher’s algorithm which finds Longest palindromic Substring in linear time.1. first this convert input_string("xyx") into new_string("x|y|x") where oddpositions are actual input characters.2. for each character in new_string it find corresponding length and store thelength and l,r to store previously calculated info.(please look the explanationfor details)3. return corresponding output_string by removing all "|""""max_length = 0# if input_string is "aba" than new_input_string become "a|b|a"new_input_string = ""output_string = ""# append each character + "|" in new_string for range(0, length-1)for i in input_string[: len(input_string) - 1]:new_input_string += i + "|"# append last characternew_input_string += input_string[-1]# we will store the starting and ending of previous furthest ending palindromic# substringl, r = 0, 0# length[i] shows the length of palindromic substring with center ilength = [1 for i in range(len(new_input_string))]# for each character in new_string find corresponding palindromic stringstart = 0for j in range(len(new_input_string)):k = 1 if j > r else min(length[l + r - j] // 2, r - j + 1)while (j - k >= 0and j + k < len(new_input_string)and new_input_string[k + j] == new_input_string[j - k]):k += 1length[j] = 2 * k - 1# does this string is ending after the previously explored end (that is r) ?# if yes the update the new r to the last index of thisif j + k - 1 > r:l = j - k + 1 # noqa: E741r = j + k - 1# update max_length and start positionif max_length < length[j]:max_length = length[j]start = j# create that strings = new_input_string[start - max_length // 2 : start + max_length // 2 + 1]for i in s:if i != "|":output_string += ireturn output_stringif __name__ == "__main__":import doctestdoctest.testmod()"""...a0...a1...a2.....a3......a4...a5...a6....consider the string for which we are calculating the longest palindromic substring isshown above where ... are some characters in between and right now we are calculatingthe length of palindromic substring with center at a5 with following conditions :i) we have stored the length of palindromic substring which has center at a3 (starts atl ends at r) and it is the furthest ending till now, and it has ending after a6ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)iv) a1 is corresponding equal character of a5 in palindrome with center a3 (rememberthat in below derivation of a4==a6)now for a5 we will calculate the length of palindromic substring with center as a5 butcan we use previously calculated information in some way?Yes, look the above string we know that a5 is inside the palindrome with center a3 andpreviously we have have calculated thata0==a2 (palindrome of center a1)a2==a4 (palindrome of center a3)a0==a6 (palindrome of center a3)so a4==a6so we can say that palindrome at center a5 is at least as long as palindrome at centera1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3so finally ..len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), r-a5)where a3 lies from l to r and we have to keep updating thatand if the a5 lies outside of l,r boundary we calculate length of palindrome withbruteforce and update l,r.it gives the linear time complexity just like z-function"""
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。