2000: Longest common child ascent sequence
- View
- Submit
- Statistics
- Questions
-
Total time limit:
-
10000ms
-
Memory Limit:
-
65536kB
-
-
Describe
-
-
given a sequence of two integers, write a program that asks for their longest ascending common subsequence.
When the following conditions are met, we will length n sequence S1, S2, ..., SN called sequence of length M A1, A2, ..., am ascending sub-sequence:
There are 1 <= i1 < I2 < ... < in <= M, so that for all 1 <= J <=n, there is SJ = Aij, and for all 1 <= J < N, there are SJ < sj+1.
-
-
Input
-
-
each sequence is represented by two lines, the first line is the length m (1 <= M <= 500), and the second row is the M integer AI of the sequence ( -231 <= ai < 231)
-
-
Output
-
-
in the first line, the length of the longest ascending common subsequence of two sequences is output L. In the second row, output the subsequence. If there is more than one sub-sequence that matches the condition, either of the outputs can be output.
-
-
Sample input
-
-
51 4 2 5-124-12 1 2 4
-
-
Sample output
-
21 4
/*the longest ascending common subsequence F (i1,i2) represents A1 with A2[0..i2], the longest ascending common subsequence ending with a1[i1] if A2[I2]==A1[I1], F (i1,i2) =max{f (i,i2-1)}+1 (0<=i<i1 If A2[i2]<a1[i1],f (I1,I2) is unchanged if A2[i2]>a1[i1],f (I1,I2) is unchanged, the value of max{F (I,I2)} is updated*/#include<cstdio>#include<vector>using namespacestd;Const intmaxn=510;intM1,M2,A1[MAXN],A2[MAXN];structnode{intLen; Vector<int>IV;} Ans[maxn],cur; intMain () {scanf ("%d",&M1); for(intI=0; i<m1;i++) scanf ("%d",&A1[i]); scanf ("%d",&m2); for(intI=0; i<m2;i++) scanf ("%d",&A2[i]); for(intI=0; i<maxn;i++) ans[i].len=0; for(intI2=0; i2<m2;i2++) {Cur.len=0; Cur.iv.clear (); for(inti1=0; i1<m1;i1++){ if(A2[i2]>a1[i1]&&ans[i1].len>cur.len) cur=ANS[I1]; if(a2[i2]==A1[i1]) {ANS[I1]=cur; ans[i1].len++; Ans[i1].iv.push_back (A1[i1]); } } } intp=0; for(intI=1; i<m1;i++){ if(Ans[p].len<ans[i].len) p=i; } printf ("%d\n", Ans[p].len); if(Ans[p].iv.size ()) {printf ("%d", ans[p].iv[0]); for(intI=1; I<ans[p].iv.size (); i++) printf ("%d", Ans[p].iv[i]); } printf ("\ n"); return 0;}
2000: Longest common child rise sequence