题解 | #公共子串计算#

公共子串计算

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

主要是用for循环嵌套截取小串的子串,并判断大串是否包含该子串,并求出各个子串长度的最大值

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
        String lStr = str1.length() > str2.length() ? str1 : str2;
        String sStr = str1.length() < str2.length() ? str1 : str2;
        int max = 0;
        for (int i = 0; i < sStr.length(); i++) {
            for (int j = i + 1; j <= sStr.length(); j++) {
                String substring = sStr.substring(i, j);
                if (lStr.contains(substring)) {
                    max = substring.length() > max ? substring.length() : max;
                }
            }
        }
        System.out.println(max);
    }
}


全部评论

相关推荐

Gardenia06...:刚开始学是这样的,可以看看左神和灵神都讲的不错
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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