50tps求方法
查看原帖
50tps求方法
671208
control_our_own_life楼主2023/7/21 16:53
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e3 + 5;
int a[N], b[N], n, dp[N][N];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++)
		scanf("%d", &b[i]);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (a[i] != b[j])
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			else
				dp[i][j] = dp[i - 1][j - 1] + 1;
		}
	}
	printf("%d", dp[n][n]);
	return 0;
}/*
5
1 2 3 2 1
5
3 2 1 4 7
answer:
3
*/

我的方法是n^2,过不了,求调……

2023/7/21 16:53
加载中...