题解 | #公共子串计算#

公共子串计算

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

public class Main { public static void main(String[] args){

     Scanner in = new Scanner(System.in);
    while(in.hasNext()){
        String line1 = in.nextLine();
        String line2 = in.nextLine();
        int count=0;
        int len1=line1.length();
        int len2 =line2.length();
        if (len1>len2){
            count = getCommonCount(line2,line1);
        }else{
            count = getCommonCount(line1,line2);
        }

        System.out.println(count);
    }
}

  public static  int getCommonCount(String min,String max){
     int length=0;
    for (int i = 0;i<min.length();i++){
        for (int j = min.length()-1;j>=i;j--){
            String sb1 = min.substring(i,j+1);
            if (max.contains(sb1)){
                if(sb1.length()>length){
                    length=sb1.length();
                }
            }
        }
    }return length;
}

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务