Given a sequence, find the length of the longest palindromic subsequence in it. For example, if the given sequence is “BBABCBCAB”, then the output should be 7 as “BABCBAB” is the longest palindromic subseuqnce in it. “BBBBB” and “BBCBB” are also palindromic subsequences of the given sequence, but not the longest ones.
// Everay single character is a palindrom of length 1 L(i, i) = 1 for all indexes i in given sequence // IF first and last characters are not same If (X[i] != X[j]) L(i, j) = max{L(i + 1, j),L(i, j - 1)} // If there are only 2 characters and both are same Else if (j == i + 1) L(i, j) = 2 // If there are more than two characters, and first and last // characters are same Else L(i, j) = L(i + 1, j - 1) + 2
Solution 1:
递归。
public int lps_recursive(String s) { return lps(s, 0, s.length()-1); } private int lps(String s, int i, int j) { if(i > j) return 0; if(i == j) return 1; if(s.charAt(i) == s.charAt(j)) { return lps(s, i+1, j-1) + 2; } else { return Math.max(lps(s, i+1, j), lps(s, i, j-1)); } }
Solution 2:
动态规划。
public int lps(String s) { int n = s.length(); // f[i][j]: longest panlindrome seq from i to j int[][] f = new int[n][n]; for(int i=0; i<n; i++) { f[i][i] = 1; } // sliding window size for(int w=2; w<=n; w++) { for(int i=0; i<=n-w; i++) { int j = i+w-1; if(s.charAt(i) == s.charAt(j)) { f[i][j] = (i+1>j-1?0:f[i+1][j-1]) + 2; } else { f[i][j] = Math.max(f[i+1][j], f[i][j-1]); } } } return f[0][n-1]; }
相关推荐
java java_leetcode题解之Longest Chunked Palindrome Decomposition.java
Longest Palindrome time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Returning back to problem solving, Gildong is now studying about ...
Longest Palindrome 题目 Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings ...
LMS Longest Monotonically Increasing Sequence Algorithm
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Java AC 版本
这是动态规划中,求最长公共子序列(Longest common string)的源代码。自己编写执行。程序简单,有注释。
北大POJ2533-Longest Ordered Subsequence【O(nlogn)】
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...
Pku acm 第2533题 Longest Ordered Subsequence 代码,有详细的注释,动态规划
Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...
最长公共子序列(Longest Common Subsequence, LCS)问题是计算机科学领域中一个重要的经典问题,它不仅被广泛应用于文本比较、生物信息学中的基因序列比对等领域,而且也是动态规划算法的一个典型应用场景。...
数据结构问题:“最长回文子串问题”(Longest Palindromic Substring),详细解法和代码实现; 数据结构问题:“最长回文子串问题”(Longest Palindromic Substring),详细解法和代码实现; 数据结构问题:“最长...
printf("The length of the longest ordered subsequence is: %d\n", longestOrderedSubsequence(nums, numsSize)); return 0; } ``` 在上面的C程序中,我们首先定义了动态规划数组`dp`,然后通过两层循环来填充...
【Longest Common Ancestor (LCA)问题】是图论中的一个重要概念,特别是在树结构的处理中。给定树T中的两个节点u和v,LCA指的是离根节点最远的公共祖先,即同时是u和v祖先的那个节点。解决LCA问题可以转化为求解...
标题“POJ2533-Longest Ordered Subsequence”是指北京大学在线判题系统POJ上的一道编程题目,其核心任务是寻找一个序列中最长的有序子序列。描述中的“解题报告+AC代码”表明这个压缩包包含了对这道问题的解答思路...
LCS(longest common substring)算法,即最大公共子串,它是求两个字符串最长公共子串的问题。大体解法是用一个矩阵来记录两个字符串中所有位置的两个字符之间的匹配情况,若是匹配则为1,否则为0。然后求出对角线最长...
get_longest_palindrome ( string ) 返回最长的子字符串,即回文。 首先生成主字符串的所有子字符串。 我们遍历这些子字符串,并检查它是否比当前最长的子字符串长,并且是否是回文。 如果两个条件都满足,则该子...
java java_leetcode题解之Longest Substring Without Repeating
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. ...
java java_leetcode题解之Longest Valid Parentheses.java