银河旅人u7ivq 题解分享 · 2025/6/14
最长公共子序列 - 题解
``` #include <bits/stdc++.h> using namespace std; #define int long long const int N = 220; int dp[N][N]; signed main(){ ios::sync_with_stdio(false); cin.tie(0) , cout.tie(0); string s1 , s2; cin >> s1 >> s2; int n = s1.length() , m = s2.length(); s1 = "#" + s1 , s2 = "#" + s2; for(int i = 1 ; i <= n ; i ++){ for(int j = 1 ; j <= m ; j ++){ if(s1[i] == s2[j]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j] , dp[i][j - 1]); } } cout << dp[n][m] << '\n'; return 0; } ```
查看全文
0 0 0 4
肥肠肥肠 题解分享 · 2024/4/12
最长公共子序列 - 题解
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ``` String x=sc.next(); String y=sc.next(); int[][] dp=new int[x.length()+1][y.length()+1]; for (int i = 0; i < x.length(); i++) { for (int j = 0; j <y.length(); j++) { if(x.charAt(i)==y.charAt(j)) { dp[i+1][j+1]=dp[i][j]+1; }else { dp[i+1][j+1]=Math.max(dp[i][j+1],dp[i+1][j]); } } } System.out.println(dp[x.length()][y.length()]); } ``` }
查看全文
0 0 0 2