Editorial for Longest Palindromic Subsequence
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Approach
Let be the length of the longest palindromic subsequence inside the substring
.
If , then these two characters can be used as the matching ends of a palindrome, so
. Otherwise, at least one of the two ends is not used, so
.
Process from right to left and
from left to right. The implementation below stores only
one row of the dynamic program, keeping the old value of
in a variable before it
is overwritten. The time complexity is
.
Solution (Python)
import sys
def longest_palindromic_subsequence(s: str) -> int:
n = len(s)
dp = [0] * n
for i in range(n - 1, -1, -1):
previous_diagonal = 0
dp[i] = 1
current = s[i]
for j in range(i + 1, n):
old = dp[j]
if current == s[j]:
dp[j] = previous_diagonal + 2
elif dp[j - 1] > dp[j]:
dp[j] = dp[j - 1]
previous_diagonal = old
return dp[-1]
def main() -> None:
data = sys.stdin.read().split()
n = int(data[0])
s = data[1]
assert len(s) == n
print(longest_palindromic_subsequence(s))
if __name__ == "__main__":
main()
Comments