As we all know, a palindrome is a word that equals
its reverse. Here are some examples of palindromes:
malayalam, gag, appa, amma.
We consider any sequence consisting of the letters of the English alphabet to be a word. So axxb,abbba and bbbccddx are words for our purpose. And aaabbaaa, abbba and bbb are examples of palindromes.
By a subword of a word, we mean a contiguous subsequence of the word. For example the subwords of the word abbba are a, b, ab, bb, ba, abb, bbb, bba, abbb, bbba and abbba.
In this task you will given a word and you must find the longest subword of this word that is also a palindrome.
For example if the given word is abbba then the answer is abbba. If the given word is abcbcabbacba then the answer is bcabbacb.
We consider any sequence consisting of the letters of the English alphabet to be a word. So axxb,abbba and bbbccddx are words for our purpose. And aaabbaaa, abbba and bbb are examples of palindromes.
By a subword of a word, we mean a contiguous subsequence of the word. For example the subwords of the word abbba are a, b, ab, bb, ba, abb, bbb, bba, abbb, bbba and abbba.
In this task you will given a word and you must find the longest subword of this word that is also a palindrome.
For example if the given word is abbba then the answer is abbba. If the given word is abcbcabbacba then the answer is bcabbacb.
Solution hint
Any subword of w that is a palindrome is also a subword when w is reversed.Input format
The first line of the input contains a single integer N indicating the length of the word. The following line contains a single word of length N, made up of the letters a,b,…, z.Output format
The first line of the output must contain a single integer indicating the length of the longest subword of the given word that is a palindrome. The second line must contain a subword that is a palindrome and which of maximum length. If there is more than one subword palindrome of maximum length, print the one that is lexicographically smallest (i.e., smallest in dictionary order).Test Data:
You may assume that 1 ≤ N ≤ 5000. You may further assume that in 30% of the inputs 1 ≤ N ≤ 300.Example:
We illustrate the input and output format using the above examples:Sample Input 1:
5 abbba
Sample Output 1:
5 abbba
Sample Input 2:
12 abcbcabbacba
Sample Output 2:
8 bcabbacb
n=input() w=input() def longestPalSubstr(string): maxLength = 1 k=0 start = 0 length = len(string) low = 0 high = 0 # One by one consider every character as center point of # even and length palindromes for i in range(1, length): # Find the longest even length palindrome with center # points as i-1 and i. low = i - 1 high = i while low >= 0 and high < length and string[low] == string[high]: if high - low + 1 > maxLength: start = low maxLength = high - low + 1 low -= 1 high += 1 # Find the longest odd length palindrome with center # point as i low = i - 1 high = i + 1 while low >= 0 and high < length and string[low] == string[high]: if high - low + 1 > maxLength: start = low maxLength = high - low + 1 low -= 1 high += 1 #print "Longest palindrome substring is:", k=maxLength print(k) print(string[start:start + maxLength])
Thanks
Happy Programming !
The is not giving correct results.
ReplyDeleten=input()
Deletew=input()
def longestPalSubstr(string):
maxLength = 1
k=0
start = 0
length = len(string)
low = 0
high = 0
# One by one consider every character as center point of
# even and length palindromes
for i in range(1, length):
# Find the longest even length palindrome with center
# points as i-1 and i.
low = i - 1
high = i
while low >= 0 and high < length and string[low] == string[high]:
if high - low + 1 > maxLength:
start = low
maxLength = high - low + 1
low -= 1
high += 1
# Find the longest odd length palindrome with center
# point as i
low = i - 1
high = i + 1
while low >= 0 and high < length and string[low] == string[high]:
if high - low + 1 > maxLength:
start = low
maxLength = high - low + 1
low -= 1
high += 1
#print "Longest palindrome substring is:",
k=maxLength
print(k)
print(string[start:start + maxLength])
longestPalSubstr(w)
its not working could give the correct answer plzz..
Deletedef substring():
ReplyDeleten = int(input())
s = input()
sr = s[::-1]
pn = 0
ps = ""
for i in range(n+1):
for j in range(i+1, n+1):
palin = s[i:j]
if sr.find(palin)!=-1:
if (pn < len(palin)) or (pn == len(palin) and ps < palin):
pn = len(palin)
ps = palin
print(pn)
print(ps)
substring()
thanks bhau
Deleteanyone give the answer plzzz...
ReplyDeletegive this answer if any one can plzzz
ReplyDeletedef longestPalindrome():
Deleten = int(input())
s = input()
temp = ""
for i in range(len(s)):
for j in range(len(s)-1,i-1,-1):
if s[i] == s[j]:
m = s[i:j+1]
if m == m[::-1]:
if len(temp) <= len(m):
temp = m
print(len(temp))
print(temp)
longestPalindrome()
### Works 100%