Update palindrome.py
This commit is contained in:
@@ -1,24 +1,28 @@
|
|||||||
def kmp_algorithm(string):
|
def palindrom(string):
|
||||||
n = len(string)
|
l, i = len(string), 0
|
||||||
lps = [None] * n
|
j = l - 1
|
||||||
l, lps[0], i = 0, 0, 1 while i < n:
|
while i <= j:
|
||||||
if string[i] == string[l]:
|
if string[i] != string[j]:
|
||||||
l += 1 lps[i] = l
|
return False
|
||||||
i += 1 else:
|
i += 1
|
||||||
if l != 0:
|
j -= 1
|
||||||
l = lps[l-1]
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
s, nr = input(), 0
|
||||||
|
s1 = s
|
||||||
|
while len(s) > 0:
|
||||||
|
if palindrom(s):
|
||||||
|
flag = 1
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
lps[i] = 0 i += 1 return lps
|
nr += 1
|
||||||
|
s = s[:-1]
|
||||||
|
|
||||||
|
if nr == 0:
|
||||||
def minadded(string):
|
print(s)
|
||||||
revstr = string[::-1]
|
else:
|
||||||
big_str = string + '$' + revstr
|
ans = s1[-nr:]
|
||||||
lps = kmp_algorithm(big_str)
|
ans = ans[::-1]
|
||||||
return len(string) - lps[-1]
|
ans = ans + s1
|
||||||
|
|
||||||
|
|
||||||
s = input()
|
|
||||||
nr = minadded(s)
|
|
||||||
ans = s[-nr:] + s
|
|
||||||
print(ans)
|
print(ans)
|
||||||
|
|||||||
Reference in New Issue
Block a user