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

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

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

import java.util.Scanner;
public class Main {
    public static void main (String []args){
       Scanner sc= new Scanner(System.in);
        while(sc.hasNext()){
            System.out.println(comStr(sc.nextLine(),sc.nextLine()));
        }
        sc.close();
    }    
    public static String comStr(String str1,String str2){
        String small = str1.length()<str2.length()?str1:str2;
        String big = str1.length()<str2.length()?str2:str1;
        int len =0;
        int []index =new int[2];
        for(int i =0; i<small.length();i++){
            for(int j = small.length();j>i;j--){
                if(big.contains(small.substring(i,j)) && (j-i > len)){
                    len = j-i;
                    index[0] = j;
                    index[1] = i;
                    break;
                }
            }
        }
        return small.substring(index[1],index[0]);
    }
}
全部评论

相关推荐

程序员牛肉:继续沉淀吧同学,你这就是纯纯的流水线产品。 差不多的学历+两个烂大街项目。自身学历又不行,现在找啥实习呢。有点太浮躁了。多花点心思搞搞ai,开源和八股。这比你这段时间捣鼓一段小厂实习要好得多;
点赞 评论 收藏
分享
10-10 11:38
已编辑
湖南理工大学 Java
小浪_Coding:多沟通叭, 公式简历+学历一般的话难找
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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