The main idea is to give a standard answer that consists of the sequence of historical events, and some of the answers to the test papers, so that you have the correct number, that is, the maximum common sub-sequence length of two sequences.
Topic Idea: Classic model, State transfer equation: A[i]==b[i], DP (x, y) = DP (x-1,y-1) +1, otherwise DP (x, y) = Max (DP (X-1,Y), DP (X,Y-1))
Topic Pit Point: Not for you to ask for the maximum common subsequence length of the two series. But to first transform, to
3 1 2 4 9 5 10 6 8 7 For example, this means that the first event occurs, the first occurrence of the second event, and the first item of the rearrangement sequence should be 2, which is the number of the event that represents the first occurrence.
Here's the code: there's something that can be improved: adding a function to the input and conversion process.
#include <iostream> #include <stdio.h> #include <fstream> #include <iomanip> #include <cmath > #include <string> #include <string.h> #include <sstream> #include <cctype> #include < climits> #include <set> #include <map> #include <queue> #include <vector> #include < iterator> #include <algorithm> #include <stack> #include <functional>/*int type Max Int_max,
The short maximum value is short_max long long and the maximum value is long_long_max*///cout << "OK" << Endl;
#define _CLR (x) memset (x,-1,sizeof (x)) using namespace Std;
const int INF = Int_max;
Const double EPS = 1e-8;
Const double EULER = 0.577215664901532860;
Const double PI = 3.1415926535897932384626;
Const double E = 2.71828182845904523536028;
typedef long Long LL;
int n,a[100],b[100],ans[100],ans2[100],res[100][100]; int dp (int x,int y) {if (x = = -1| |
y = =-1) return 0;
if (res[x][y]! =-1) return res[x][y]; if (a[x] = = Ans[y]) return res[x][y] = DP (x-1, y-1) +1;
else return res[x][y] = max (DP (X-1,Y), DP (X,Y-1));
} int main () { //freopen ("Sample.in", "R", stdin);
Freopen ("Sample.out", "w", stdout);
CIN >> N;
for (int i = 0;i<n;i++) cin >> B[i];
for (int i = 0;i<n;i++) a[b[i]-1] = i+1;
for (int i = 0;i<n;i++) cout << a[i] << "";
cout << Endl;
while (Cin >> ans2[0]) {_CLR (res);
for (int i = 1;i<n;i++) cin >> Ans2[i];
for (int i = 0;i<n;i++) ans[ans2[i]-1] = i+1;
cout << dp (n-1,n-1) << Endl;
} //fclose (stdin);
//fclose (stdout); return 0;
}