题解 | #最长回文子序列#

最长回文子序列

https://www.nowcoder.com/practice/82297b13eebe4a0981dbfa53dfb181fa

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
        // in.nextLine();
            String str = in.nextLine();
            int len=str.length();
            int[][] dp= new int[len][len];
            long res=Long.MIN_VALUE;

            for(int i=0;i<len;i++){
                dp[i][i]=1;
            }
            for(int i=len-1;i>=0;i--){
                for(int j=i+1;j<len;j++){
            if(str.charAt(i) ==str.charAt(j))dp[i][j] = dp[i+1][j-1] + 2;
            else dp[i][j] = Math.max(dp[i][j-1],dp[i+1][j]);
                }

            }


            System.out.println(dp[0][len-1]);
        }
    }
}

全部评论

相关推荐

07-14 12:22
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务