题解 | #公共子串计算#

公共子串计算

https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
     public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
         String a = "", b = "";
         int out = 0;
         // 注意 hasNext 和 hasNextLine 的区别
         while (in.hasNextLine()) { // 注意 while 处理多个 case
             a = in.nextLine();
             b = in.nextLine();
            
        }
         for (int starti = 0; starti < a.length(); starti++) {
             for (int endi = starti+1; endi <= a.length(); endi++) {
                 if (b.contains(a.substring(starti, endi))) {
                     int outi = endi - starti;
                     if (out < outi) {
                         out = outi;
                    }
                }
            }
        }
         System.out.println(out);
    }
}

全部评论

相关推荐

快刀斩offer:干测试,项目组就我一个测试,准备在职考研跑路了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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