题解 | #密码截取#

密码截取

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

import java.util.Scanner;

// 动态规划
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int len = s.length();
        boolean[][] a = new boolean[len][len];
        int max = 0;
        for (int i = 0; i < len; i++) {
            a[i][i] = true;
        }
        for (int x = 1; x < len; x++) {
            for (int y = 0; y < x; y++) {
                if (s.charAt(x) == s.charAt(y) && (x - y <= 2 || a[x - 1][y + 1] == true)) {
                    a[x][y] = true;
                    max = Math.max(x - y + 1, max);
                }
            }
        }
        System.out.println(max);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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