题解 | #公共子串计算#

公共子串计算

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);
        while (in.hasNext()) {
            String str1 = in.nextLine().toLowerCase();
            String str2 = in.nextLine().toLowerCase();
            if (str1.length() > str2.length()) {
                String gg = str1;
                str1 = str2;
                str2 = gg;
            }
            int sum = 0;
            for (int i = 0 ; i < str1.length(); i++) {
                for (int j = str1.length(); j > i; j--) {
                    String gg = str1.substring(i, j);
                    if (str2.contains(gg)) {
                        if (j - i > sum) sum = j - i;
                    }
                }
            }
            System.out.println(sum);
        }
    }
}

全部评论

相关推荐

03-03 23:42
复旦大学 Java
tongx_:闹呢,这找不到其他人还活不活
点赞 评论 收藏
分享
01-26 19:51
门头沟学院 Java
isabener:怎么感觉像群发的呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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