返回题解分享
讨论 / 题解分享/ 帖子详情

最长公共子序列 - 题解

#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 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!