IME++ Starters Try-outs 2019

Building Bridges

原题链接
最长公共子序列,动态规划
参考代码:

    public static void main(String[] args) {
    
    
        Scanner sc=new Scanner(System.in);
        char[] s1 = sc.next().toCharArray();
        char[] s2 = sc.next().toCharArray();
        int[][] dp = new int[s1.length + 1][s2.length + 1];

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

                if(s1[i - 1] == s2[j - 1]){
    
    
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else{
    
    

                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        System.out.println(dp[s1.length][s2.length]);

    }

猜你喜欢

转载自blog.csdn.net/qq_44900959/article/details/110441610