Test instructions
is to ask for the longest common subsequence of two sequences.
Ideas:
The input of this topic is very pit father, if the input understanding clearly, this topic is not difficult. The input of the topic indicates where the number in the position is placed, for example, the input is 1,3,2,4 its corresponding sequence should be 1,3,2,4;
Here are 2 code, one is the classic solution, and one is the code that I wrote today to turn the problem into the longest-way solution on a dag graph.
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int d[30],n,map[30][30]; int dp (int i) {if (d[i]>0) return d[i]; D[i]=1; for (int j=1;j<=n;j++) if (Map[i][j]) {int T=DP (j) +1; if (d[i]<t) d[i]=t; } return d[i];} void Init () {memset (d,0,sizeof (d)); memset (map,0,sizeof (map));} int main () {int i,j,a[30],b[30]; while (scanf ("%d", &n)!=eof) {int x; for (i=1;i<=n;i++) {scanf ("%d", &x); A[x]=i; } while (scanf ("%d", &x)!=eof) {Init (); b[x]=1; for (i=2;i<=n;i++) {scanf ("%d", &x); B[x]=i; } for (i=1;i<=n;i++) {for (j=1;j<=n;j++) if (B[i]==a[j]) Break for (int k=i+1;k<=n;k++) for (inT d=j;d<=n;d++) if (b[k]==a[d]) map[i][k]=1; } int ans=0; for (i=1;i<=n;i++) Ans=max (ANS,DP (i)); printf ("%d\n", ans); }} return 0;}
#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int main () {int i,j,a[30], B[30],dp[30][30],n; while (scanf ("%d", &n)!=eof) {int x; for (i=1;i<=n;i++) {scanf ("%d", &x); A[x]=i; } while (scanf ("%d", &x)!=eof) {b[x]=1; for (i=2;i<=n;i++) {scanf ("%d", &x); B[x]=i; } memset (Dp,0,sizeof (DP)); for (i=1;i<=n;i++) for (j=1;j<=n;j++) {if (A[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; else Dp[i][j]=max (dp[i-1][j],dp[i][j-1]); } int ans=0; for (i=1;i<=n;i++) for (j=1;j<=n;j++) Ans=max (Ans,dp[i][j]); printf ("%d\n", ans); }} return 0;}
Uva--111history Grading +DP