题解 | #公共子串计算#

公共子串计算

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String  a = in.next();
            String  b = in.next();
            String  big = a.length()>b.length()?a:b;
            String  small = a.length()>b.length()?b:a;
            int max = 0; 
            if(big.contains(small)){
                System.out.println( small.length());
                continue;
            }
            for(int i = 0 ;i <small.length()-1 ;i++){

                for(int j = i+1 ; j<small.length() ;j++){

                    String res = small.substring(i,j);
                    if(big.contains(res)){
                        max = Math.max(res.length(),max);
                    }
                }
            }
            System.out.println( max );
        }
    }
}

全部评论

相关推荐

dao_yi:投了1000个左右,回消息的很少,要简历然后说过几天联系的都没有消息了,约面试的基本都是3000左右,足够在当地生活,最后去了一个武汉的3000,干了两天回来考研了,感觉这个行业加班是常态,看能不能考研上岸找个国企,或者大厂。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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