题解 | #Where in 和Not in#

公共子串计算

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

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str1 = in.readLine(); String str2 = in.readLine(); int m = str1.length(); int n = str2.length(); int max = 0;

    int[][] dp = new int[m][n];
    for(int i = 0;i <n;i++){
        if(str1.charAt(0) == str2.charAt(i)){
            dp[0][i] = 1;
            max = Math.max(max,dp[0][i]);
        }
        
    }
    for(int i = 0;i <m;i++){
        if(str1.charAt(i) == str2.charAt(0)){
            dp[i][0] = 1;
            max = Math.max(max,dp[i][0]);
        }
        
    }
    
    for(int i = 1;i < m;i++){
        for(int j = 1;j < n;j++){
            if(str1.charAt(i) == str2.charAt(j) ){
                dp[i][j] = dp[i-1][j-1] +1;
                max = Math.max(max,dp[i][j]);
            }else{
                dp[i][j] = 0;
            }
        }
    }
System.out.println(max);
}

}

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

06-20 14:27
中山大学 C++
rt,day3就开始接需求
星际探神:你就想 你是水货他们都没面出来 他们也水 管他呢
点赞 评论 收藏
分享
明天不下雨了_人机版:让我们大声的说出来:以前的未来就是现在
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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