2 条题解
-
0
#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
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()]); }
}
- 1
信息
- ID
- 126
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 287
- 已通过
- 141
- 上传者