题解 | #密码截取#

密码截取

https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();

        int n = str.length();
        boolean[][] dp = new boolean[n][n];

        int max = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j <= i; j++){
                if(str.charAt(j) == str.charAt(i) && (i - j < 2 || dp[j + 1][i - 1] == true)){
                    dp[j][i] = true;
                    max = Math.max(max,i - j + 1);
                }
            }
        }

        System.out.println(max);
    }
}

全部评论

相关推荐

10-16 23:37
已编辑
门头沟学院 运营
据说名字越长别人越关...:西交做运营吗。。就业市场已经颠了
简历被挂麻了,求建议
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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