题解 | #查找两个字符串a,b中的最长公共子串#

查找两个字符串a,b中的最长公共子串

https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s1 = in.nextLine();
        String s2 = in.nextLine();
        int max = 0;
        if (s1.length() > s2.length()) {
            String temp = s1;
            s1 = s2;
            s2 = temp;
        }
        String res = "";
        for (int i = 0; i < s1.length(); i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = i; j < s1.length(); j++) {
                sb.append(s1.charAt(j));
                if (s2.contains(sb.toString()) && sb.length() > max) {
                    max = sb.length();
                    res = sb.toString();
                }
            }
        }
        System.out.print(res);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
2025-12-05 23:10
点赞 评论 收藏
分享
COLORSN:可以试一下,小厂看技术栈是不是很落后,如果太拉胯就别去,个人认为有实习氛围比你自己琢磨要高效不少,然后就是小厂其实也有可能会问的很难,这都比较难说,还是看自己项目含金量够不够,寒假还能不能推进学习再选择,毕竟去实习过年就10天假了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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