Brief Description:
A history exam, there are n historical events, and the years between them are different, and students are asked to arrange them in the correct order. There are two ways to score, using the second: Assuming that there are historical events 1,2,3,4, their correct chronological order is 1,2,3,4, and then assuming the student's answer is 1,3,2,4, then according to the correct number of relative order, answer three (1,2,4 or 1,3,4), That is, the longest common subsequence length with the correct answer is 3, which is the number of answers.
Analyse:
The longest common sub-sequence template problem, but the input is a big hole, his input is in order, Event 1 is ranked in the first few, Event 2 is ranked in the number of ..., so you have to first convert the input into the correct order.
CODE:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <vector> #include <string> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #define INF 0x7fffffff#define SUP 0x80000000#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int n=100007;int st[22],t[22];int dp[22][22];int Main () {int n; int id; scanf ("%d", &n); for (int i=1;i<=n;i++) {scanf ("%d", &id); St[id]=i; } while (scanf ("%d", &id) ==1) {t[id]=1; for (int i=2;i<=n;i++) {scanf ("%d", &id); T[id]=i; } mem (dp,0); for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (St[i]==t[j]) dp[i][j]=dp[i-1][j-1]+1; else {Dp[i][j]=max (Dp[i][j],max (dp[i-1][J],DP[I][J-1])); }}} printf ("%d\n", Dp[n][n]); } return 0;}
UVA 111 History Grading "LCS"