Mots-clés » D.P.
So it’s been a while since I’ve posted anything (again) as the day job (and indeed one of the ‘other’ job’s) has been taking up a large chunk of my time. 490 mots de plus
Maintain a nxn boolean table, table[i][j] is true when a is palindromes
O(n^2) time, O(n^2) space
def longestpalindromic(a): n = len(a) table = [ for x in xrange(n)] maxLength = 1 # All substrings of length 1 are true for i in xrange(n): table[i][i] = 1 # Check for substring of length 2 for i in xrange(n-1): if a[i] == a: table[i] = 1 start = i maxLength = 2 # check substring greater than 2 for k in xrange(3, n+1): # k is length of substring for i in xrange(n-k+1): # i is starting index j = i + k - 1 # j is ending index if table != 0 and a[i] == a[j]: table[i][j] = 1 if k > maxLength: start = i maxLength = k return a 14 mots de plus