📜  门| GATE-CS-2003 |问题 22(1)

📅  最后修改于: 2023-12-03 15:28:41.817000             🧑  作者: Mango

GATE-CS-2003 Problem 22

This problem involves finding the longest palindrome that can be obtained by removing zero or more characters from a given string. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.

Example

For example, the longest palindrome that can be obtained by removing zero or more characters from the string "racecar" is "racecar". If we remove the 'e' character, we get "raccar", which is also a palindrome, but not as long as "racecar".

Solution

One approach to solving this problem is to use dynamic programming to compute the lengths of the longest palindromic substrings for each possible substring of the input string.

We can initialize a two-dimensional array dp of size n x n, where n is the length of the input string, to store the lengths of the longest palindromic substrings. The first dimension of the array represents the starting index of the substring, and the second dimension represents the ending index of the substring.

We can start by initializing the diagonal elements of the array to 1, since any single character is a palindrome of length 1. Then, we can iterate over the array diagonally, filling in the values for the substrings that are one character longer than the previous ones.

For example, to compute the value of dp[i][j], we can check whether the characters at index i and j are the same. If they are, then we can look up the value of dp[i+1][j-1] to see if the substring between i+1 and j-1 is also a palindrome, and if so, we can set dp[i][j] to dp[i+1][j-1] + 2 (i.e. the length of the substring between i and j plus 2). Otherwise, we can set dp[i][j] to the maximum of dp[i+1][j] and dp[i][j-1].

After filling in the entire array, we can find the maximum value in dp to get the length of the longest palindromic substring. To obtain the actual substring, we can use the indices of the maximum value to backtrack through the array and construct the substring.

Code

Here is the Python code implementing the dynamic programming algorithm described above:

def longest_palindrome(s):
    n = len(s)
    dp = [[0] * n for _ in range(n)]

    # Initialize diagonal elements to 1
    for i in range(n):
        dp[i][i] = 1

    # Fill in array diagonally
    for k in range(1, n):
        for i in range(n-k):
            j = i + k
            if s[i] == s[j]:
                dp[i][j] = dp[i+1][j-1] + 2
            else:
                dp[i][j] = max(dp[i+1][j], dp[i][j-1])

    # Backtrack to construct longest palindrome
    i, j = 0, n-1
    palindrome = ''
    while i < j:
        if s[i] == s[j]:
            palindrome += s[i]
            i += 1
            j -= 1
        elif dp[i+1][j] >= dp[i][j-1]:
            i += 1
        else:
            j -= 1
    if i == j:
        palindrome += s[i]

    return palindrome

This implementation has a time complexity of O(n^2), since we fill in an n x n array using nested loops. However, it has a space complexity of O(n^2) as well, since we need to store the lengths of all possible substrings in the array. To improve the space complexity, we can use a one-dimensional array to store the lengths of palindromic substrings of odd length, and another array to store the lengths of palindromic substrings of even length.