Recommend a fried chicken to praise the blog.
The following code has the print path.
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime > #include <iomanip> #pragma comment (linker, "/stack:1024000000"); #define EPS (1e-6) #define LL Long Long#define ULL unsigned long long#define _ll __int64#define INF 0x3f3f3f3f#define Mod 1000000007using namespace Std;int a[510],b[510] ; int py[510][510],px[510][510],dp[510][510] = {0};void Output (int x,int y) {if (x = =-1 && y = =-1) retur n; Output (Px[x][y],py[x][y]); if (a[x] = = B[y]) printf ("%d", a[x]);} int main () {int n,m,i,j,mlen,x,y; scanf ("%d", &n); for (i = 1;i <= n; ++i) scanf ("%d", &a[i]); scanf ("%d", &m); for (i = 1;i <= m; ++i) scanf ("%d", &b[i]); int Max = 0,ax,ay; for (i = 1;i <= n; ++i) {Mlen = 0,x = -1,y =-1; for (j = 1;j <= m; ++j) { DP[I][J] = Dp[i-1][j]; PX[I][J] = i-1,py[i][j] = j; if (B[j] < A[i] && Dp[i-1][j] > Mlen) mlen = Dp[i-1][j],x = I-1,y = j; if (b[j] = = A[i]) dp[i][j] = Mlen + 1,px[i][j] = x,py[i][j] = y; if (Dp[i][j] > max) max = Dp[i][j],ax = I,ay = j; }} if (max) printf ("%d\n", Max), Output (Ax,ay); else printf ("0\n"); return 0;}
Codeforces 10D. LCIs Longest public Ascent sub-series template title + Print path