#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
using namespace std;
const int maxn = 10001;
int a[maxn], dp[maxn];
int hashtable[maxn];
int main()
{
int n, m, x;
cin >> n >> m;
memset(hashtable, -1, sizeof(hashtable));
for (int i = 0; i < m; i++)
{
cin >> x;
hashtable[x] = i;
}
int l, num = 0;
cin >> l;
for (int i = 0; i < l; i++)
{
cin >> x;
if (hashtable[x] >= 0)
{
a[num++] = hashtable[x];
}
}
int ans = -1;
for (int i = 0; i < num; i++)
{
dp[i] = 1;
for (int j = 0; j < i; j++)
{
if (a[j] <= a[i] )
{
dp[i] = dp[j] + 1;
}
}
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
最长公共子序列
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
using namespace std;
const int maxn = 10001;
int a[maxn], b[maxn], dp[maxn][maxn];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> a[i];
}
int l; cin >> l;
for (int i = 1; i <= l; i++)
{
cin >> b[i];
}
for (int i = 0; i <= m; i++)
{
dp[i][0] = 0;
}
for (int j = 0; j <= l; j++)
{
dp[0][l] = 0;
}
int num = 0;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= l; j++)
{
int MAX = max(dp[i - 1][j], dp[i][j - 1]);
if (a[i] == b[j])
{
dp[i][j] = MAX + 1;
}
else
{
dp[i][j] = MAX;
}
}
}
cout << endl;
cout << dp[m][l] << endl;
return 0;
}