TheAlgorithms-Python

Форк
0
108 строк · 3.9 Кб
1
def palindromic_string(input_string: str) -> str:
2
    """
3
    >>> palindromic_string('abbbaba')
4
    'abbba'
5
    >>> palindromic_string('ababa')
6
    'ababa'
7

8
    Manacher’s algorithm which finds Longest palindromic Substring in linear time.
9

10
    1. first this convert input_string("xyx") into new_string("x|y|x") where odd
11
        positions are actual input characters.
12
    2. for each character in new_string it find corresponding length and store the
13
        length and l,r to store previously calculated info.(please look the explanation
14
        for details)
15

16
    3. return corresponding output_string by removing all "|"
17
    """
18
    max_length = 0
19

20
    # if input_string is "aba" than new_input_string become "a|b|a"
21
    new_input_string = ""
22
    output_string = ""
23

24
    # append each character + "|" in new_string for range(0, length-1)
25
    for i in input_string[: len(input_string) - 1]:
26
        new_input_string += i + "|"
27
    # append last character
28
    new_input_string += input_string[-1]
29

30
    # we will store the starting and ending of previous furthest ending palindromic
31
    # substring
32
    l, r = 0, 0
33

34
    # length[i] shows the length of palindromic substring with center i
35
    length = [1 for i in range(len(new_input_string))]
36

37
    # for each character in new_string find corresponding palindromic string
38
    start = 0
39
    for j in range(len(new_input_string)):
40
        k = 1 if j > r else min(length[l + r - j] // 2, r - j + 1)
41
        while (
42
            j - k >= 0
43
            and j + k < len(new_input_string)
44
            and new_input_string[k + j] == new_input_string[j - k]
45
        ):
46
            k += 1
47

48
        length[j] = 2 * k - 1
49

50
        # does this string is ending after the previously explored end (that is r) ?
51
        # if yes the update the new r to the last index of this
52
        if j + k - 1 > r:
53
            l = j - k + 1
54
            r = j + k - 1
55

56
        # update max_length and start position
57
        if max_length < length[j]:
58
            max_length = length[j]
59
            start = j
60

61
    # create that string
62
    s = new_input_string[start - max_length // 2 : start + max_length // 2 + 1]
63
    for i in s:
64
        if i != "|":
65
            output_string += i
66

67
    return output_string
68

69

70
if __name__ == "__main__":
71
    import doctest
72

73
    doctest.testmod()
74

75
"""
76
...a0...a1...a2.....a3......a4...a5...a6....
77

78
consider the string for which we are calculating the longest palindromic substring is
79
shown above where ... are some characters in between and right now we are calculating
80
the length of palindromic substring with center at a5 with following conditions :
81
i) we have stored the length of palindromic substring which has center at a3 (starts at
82
    l ends at r) and it is the furthest ending till now, and it has ending after a6
83
ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4)
84
iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6)
85
iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember
86
    that in below derivation of a4==a6)
87

88
now for a5 we will calculate the length of palindromic substring with center as a5 but
89
can we use previously calculated information in some way?
90
Yes, look the above string we know that a5 is inside the palindrome with center a3 and
91
previously we have calculated that
92
a0==a2 (palindrome of center a1)
93
a2==a4 (palindrome of center a3)
94
a0==a6 (palindrome of center a3)
95
so a4==a6
96

97
so we can say that palindrome at center a5 is at least as long as palindrome at center
98
a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3
99
so finally ..
100

101
len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), r-a5)
102
where a3 lies from l to r and we have to keep updating that
103

104
and if the a5 lies outside of l,r boundary we calculate length of palindrome with
105
bruteforce and update l,r.
106

107
it gives the linear time complexity just like z-function
108
"""
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.