题解 | #密码截取# 简便易行

密码截取

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

import java.util.*;
import java.lang.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] c = in.nextLine().toCharArray();
        int len = c.length;
        int maxLen = 0;
        // dp[i][j]表示i到j这一段是否为回文串
        boolean[][] dp = new boolean[len][len];

        // 初始化
        for(int i=0;i<len;i++){
            dp[i][i] = true;
        }

        for(int Len=2;Len<=len;Len++){
            for(int i=0;i<len;i++){
                int j = Len+i-1;
                if(j>len-1){break;}
                if(c[i] != c[j]){
                    dp[i][j] = false;
                }else{
                    if(Len<4){
                        dp[i][j] = true;
                    }else{
                        dp[i][j] = dp[i+1][j-1];
                    }
                }

                if(dp[i][j] == true && Len > maxLen){
                    maxLen = Len;
                }
            }
        }
        System.out.println(maxLen);
    }
}

全部评论

相关推荐

Rena1ssance_:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
勤劳的鲸鱼在okr拆解:没有别的选择就去吧,有实习和没实习找工作是天上地下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务