DP, meaning obscure. LCS problems.
The source sequence must be converted. For example, 4 2 1 3. Event 1 occurs in the fourth place, event 2 occurs in the second place, event 3 occurs in the first place, and event 4 occurs in the third place.
The converted sequence is 3 2 4 1.
So it is very sad. A lot of questions are about a lot of questions.
I used a two-dimensional array. In fact, you can optimize rolling or one-dimensional.
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<vector>#include<cmath>#define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,n) for(int i= a;i< n ;i++)#define FOR0(i,a,b) for(int i=a;i>=b;i--)#define pb push_back#define mp make_pair#define ft first#define sd second#define sf scanf#define pf printf#define acfun std::ios::sync_with_stdio(false)#define SIZE 1000+1using namespace std;int a[SIZE];int b[SIZE];int dp[SIZE][SIZE];int main(){ int n; sf("%d",&n); int tmp; FOR(i,0,n) { sf("%d",&tmp); a[tmp-1]=i; } while(sf("%d",&tmp)!=EOF) { b[tmp-1]=0; FOR(i,1,n) { sf("%d",&tmp); b[tmp-1]=i; } CLR(dp,0); FOR(i,0,n) FOR(j,0,n) { if(a[i]==b[j]) dp[i+1][j+1]=dp[i][j]+1; else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]); } pf("%d\n",dp[n][n]); }}
UV 111 history grading