题解 | #公共子串计算#

公共子串计算

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String line1 = in.nextLine();
            String line2 = in.nextLine();
            if(line1.length()>line2.length()) {
                String c = line1;
                line1 = line2;
                line2 = c;
            }
            int maxLen =0;
            int[][]dp = new int[line1.length()+1][line2.length()+1];
            for(int i=1;i<=line1.length();i++) {
                for(int j=1;j<=line2.length();j++) {
                    if(line1.charAt(i-1) == line2.charAt(j-1)) {
                        dp[i][j] = dp[i-1][j-1] + 1;
                        maxLen = Math.max(maxLen, dp[i][j]);
                    }
                }
            }
            System.out.println(maxLen);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
运营3年修炼中接简历辅导:你的科研项目经历里,只写了你的动作,没有写你的思考和成果,不要只写使用什么进行了什么,这等于罗列你的任务,简历是为了突出你的优秀,你在什么样的任务背景下,克服了什么样的困难,针对性地做了哪些事情,最后达成了什么成果(用数据体现你的成果和效率)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务