题解 | #公共子串计算#

公共子串计算

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

import java.util.*;

public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine();
        String b = sc.nextLine();
        int length = a.length()>b.length()?calc(a,b,b.length()):calc(b,a,a.length());
        System.out.println(length);
    }
    
    public static int calc(String  longer, String shorter, int length){
        if(length==0){
            return length;
        }
        if(longer.indexOf(shorter) >-1){
            return length;
        }
        int start = 0;
        boolean find = true;
        while(start+length <=shorter.length()){
            String str = shorter.substring(start,start+length);
            if(longer.indexOf(str) >-1){
                find = false;
                break;
            }
            start++;
        }
        if(find){
            length= calc(longer,shorter,length-1);
        }
        return length;
        
    }
}
全部评论

相关推荐

爱吃烤肠的牛油最喜欢...:50K是ssp了估计,ssp的人家多厉害都不用说,每年比例大概在百分之5左右
点赞 评论 收藏
分享
09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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