rambless

密码截取

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

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
            String str = in.nextLine();
            char[] arr = str.toCharArray();
            int[] dp = new int[arr.length];
            int max = 0;
            for(int i=0; i<arr.length-1; i++) {
                //倒叙找,找到直接break,后边的肯定更小
                for(int j=arr.length-1; j>i; j--) {
                    boolean flag = judge(str, i, j);
                    if(flag) {
                        max = Math.max(max, j-i+1);
                        break;
                    }
                }
            }
            System.out.println(max);
        }
    }

    private static boolean judge(String str, int i, int j) {
        int mid = (j-i)/2;
        for(int m=0; m<=mid; m++) {
            String pre = str.substring(i+m, i+m+1);
            String post = str.substring(j-m,j-m+1);
            if(!pre.equals(post)) {
                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

2025-12-10 19:36
湖北工业大学 Web前端
饿魔:看到在线简历了吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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