zoukankan      html  css  js  c++  java
  • lintcode667

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
    Example
    Given s = "bbbab" return 4
    One possible longest palindromic subsequence is "bbbb".
     
    DP。
    定义:dp[i][j]为s.substring(i, j+1)中最长subsequence的长度
    转移方程: if (chars[I] == chars[j]) then dp[I][j] = 2 + dp[i+1][j-1] else dp[I][j] = max(dp[i+1][j], dp[i][j-1]).
    返回值: dp[0][s.length()-1]
    原理:和substring的问题区别就在于可以跳着取,所以i,j可以不用都参与进来。对任何dp[i][j]你就分成ij都贡献,只i贡献,只j贡献,ij都不贡献四种情况,从而如以上转移方程,其中因为递增性省去了ij都不贡献的情况。
     
    细节:
    1.dp题目很多可以最后就返回dp记录的其中一项,省去自己打擂台。
    2.从右下开始往上再往右走。 
     
     
    1. 我的实现
    public class Solution {
        /**
         * @param s: the maximum length of s is 1000
         * @return: the longest palindromic subsequence's length
         */
        public int longestPalindromeSubseq(String s) {
            // write your code here
            
            int result = 0;
            if (s == null) {
                return result;
            }
            
            int[][] longestLength = new int[s.length()][s.length()];
            
            //initialization for diagonal
            for (int i = 0; i < s.length(); i++) {
                longestLength[i][i] = 1;
                result = Math.max(result, longestLength[i][i]);
            }
            
            //initialization for the line in the right of the diagonal
            for (int i = 0; i + 1 < s.length(); i++) {
                longestLength[i][i + 1] = s.charAt(i) == s.charAt(i + 1) ? 2 : 1;
                result = Math.max(result, longestLength[i][i + 1]);
            }
            
            //for general cases
            for (int i = s.length() - 1; i >= 0; i--) {
                for (int j = i + 2; j < s.length(); j++){
                    if (s.charAt(i) == s.charAt(j)) {
                        longestLength[i][j] = longestLength[i + 1][j - 1] + 2;
                    } else {
                        longestLength[i][j] = Math.max(longestLength[i + 1][j], 
                                                       longestLength[i][j - 1]);
                    }
                    result = Math.max(result, longestLength[i][j]);
                }
            }
            
            return result;
        }
    }

    2.九章实现

    public class Solution {
        /**
         * @param s the maximum length of s is 1000
         * @return the longest palindromic subsequence's length
         */
        public int longestPalindromeSubseq(String s) {
            // Write your code here
            int length = s.length();
            if (length == 0)
                return 0;
            int[][] dp = new int[length][length];
            for (int i = length - 1; i >= 0; i--) {
                dp[i][i] = 1;
                for (int j = i + 1; j < length; j++) {
                    if (s.charAt(i) == s.charAt(j)) {
                        dp[i][j] = dp[i + 1][j - 1] + 2;
                    } else {
                        dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
                    }
                }
            }
            return dp[0][length - 1];
        }
    }
  • 相关阅读:
    总有一天你将破蛹而出
    java 连接 Access数据库的两种方法
    freemarker中页面直接可以使用的内置对象
    freemarker中页面直接可以使用的内置对象
    常见的样式
    ibatis常用的集中判断语句
    mysql类型转换
    ibatis常用的集中判断语句
    window.open打开窗口时父窗口变成object
    window.open打开窗口时父窗口变成object
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9428205.html
Copyright © 2011-2022 走看看