It's an introduction to lcis. This DP Method has never been described before. Now I have learned it well. DP is really amazing...
First, DP first defines the status DP [I] [J], which indicates the first I of string a and the first J of string B, the length of lcis ending with B [J.
State transition equation:
If (A [I] = B [J]) DP [I] [J] = max (DP [I-1] [k]) + 1; (1 <= k <j)
Else DP [I] [J] = DP [I-1] [J];
Then select the cyclic order to setAlgorithmThe complexity is reduced to N * n.
Greatest common increasing subsequence
Time limit: 2 seconds Memory limit: 65536 KB Special Judge
You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal
Possible length.
Sequence S1, S2 ,..., SN of length N is called an increasing subsequence of a sequence A1, A2 ,..., am of length m if there exist 1 <= I1 <I2 <... <in <= m such that SJ = AIJ for all 1 <= j <= n, and SJ <SJ + 1 for all 1 <= j <n.
Input
Each sequence is described with m-its length (1 <= m <= 500) and M integer numbers AI (-2 ^ 31 <= AI <2 ^ 31) -The sequence itself.
Output
On the first line of the output print l-the length of the greatest common increasing subsequence of both sequences. on the second line print the subsequence itself. if there are several possible answers, output any of them.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. each input block is in the format indicated in the Problem description. there is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
5
1 4 2 5-12
4
-12 1 2 4
Sample output
2
1 4
Source: Northeastern Europe 2003, northern subregion
# Include <stdio. h> # Include < String . H> # Include <Iostream> Using Namespace STD; # Define N 550 Struct Node { Int X, Y;} path [N] [N]; Int DP [N] [N]; Int S [N], t [N]; Int Main (){ Int T1; While (Scanf ( " % D " , & T1 )! = EOF ){ While (T1 -- ) {Memset (path, 0 , Sizeof (PATH )); Int N, m; scanf ( " % D " ,& N ); For ( Int I = 1 ; I <= N; I ++) Scanf ( " % D " ,& S [I]); scanf ( " % D " ,& M ); For ( Int I = 1 ; I <= m; I ++ ) Scanf ( " % D " ,& T [I]); memset (DP, 0 , Sizeof (DP )); Int MX = 0 ; For ( Int I = 1 ; I <= N; I ++ ) {MX = 0 ; Int Tx = 0 , Ty = 0 ; For ( Int J = 1 ; J <= m; j ++ ) {DP [I] [J] = DP [I- 1 ] [J]; path [I] [J]. x = I- 1 ; Path [I] [J]. Y = J; If (S [I]> T [J] & MX <DP [I- 1 ] [J]) {MX = DP [I- 1 ] [J]; TX = I- 1 ; Ty = J ;} If (S [I] = T [J]) {DP [I] [J] = Mx + 1 ; Path [I] [J]. x = TX; path [I] [J]. Y = Ty ;}} MX =- 1 ; Int ID; For ( Int I = 1 ; I <= m; I ++ ) If (DP [N] [I]>MX) {MX = DP [N] [I]; ID = I ;} Int Save [N]; Int CNT = 0 ; Int TX, Ty; TX = N; ty = ID; While (DP [TX] [ty]! = 0 ){ Int Tmpx, tmpy; tmpx = Path [TX] [ty]. X; tmpy = Path [TX] [ty]. Y; If (DP [TX] [ty]! = DP [tmpx] [tmpy]) {save [CNT ++] = T [ty];} TX = Tmpx; ty = Tmpy;} printf ( " % D \ n " , MX ); For ( Int I = CNT- 1 ; I> = 0 ; I -- ) Printf ( " % D " , Save [I]); printf ( " \ N " );}} Return 0 ;}