Summary of LIS (longest incrementing subsequence) and LCS (longest common subsequence)
Longest Common subsequence (LCS): O (N ^ 2)
Two for loops match two strings by bit: I in range (1, len1) J in range (1, len2)
S1 [I-1] = S2 [J-1], DP [I] [J] = DP [I-1] [J-1] + 1;
S1 [I-1]! = S2 [J-1], DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]);
Initialization: DP [I] [0] = DP [0] [J] = 0;
Pseudocode:
DP [maxn1] [maxn2]; S1 [maxn1], S2 [maxn2]; P [maxn1] [maxn2] [2]; // init for I in range (0, len1): DP [I] [0] = 0; else:; for I in range (0, len2): DP [0] [I] = 0; else :; for I in range (1, len1): For J in range (1, len2): If S1 [I] = S2 [J]: DP [I] [J] = DP [I-1] [J-1] + 1; p [I] [J] [0] = I-1; P [I] [J] [1] = J-1; else: If DP [I-1] [J]> DP [I] [J-1]: DP [I] [J] = DP [I-1] [J]; P [I] [J] [0] = I-1; P [I] [J] [1] = J; else: DP [I] [J] = DP [I] [J-1]; P [I] [J] [0] = I; P [I] [J] [1] = J-1; else :; return DP [len1] [len2]; // path non-recursive function print_path (len1, len2): If (DP [len1] [len2] = 0) return; printf_path (P [len1] [len2] [0], p [len1] [len2] [1]); If S1 [len1] = S2 [len2]: printf: s1 [len1]; end function;
Question: Ultraviolet A-531 compromiseuva-10066the Twin Towers ultraviolet A-10192 vacation
Uva10405-Longest Common subsequence
Longest ascending subsequence (LIS): O (N ^ 2)
From left to right, find the length of the longest incrementing sub-sequence of the sequence with the first I length. The state transition equation is as follows:
DP [I] = max (DP [J] + 1); I in range (1, Len); j in range (1, I-1 );
Pseudocode
S [maxn], DP [maxn]; for I in range (1, Len): DP [I] = 1; for I in range (2, Len ): for J range (1, I-1): If s [I]> S [J]: DP [I] = max (DP [I], DP [J] + 1); else:; return DP [Len]; // path recursive function print_path (max_len, Len): If max_len = 0: return; for I in range (Len, 1): If DP [I] = max_len: print_path (max_len-1, I-1); printf: s [I]; end function;
Question: Ultraviolet A-10599 robots (II)
Longest incrementing sub-sequence O (N * logn)
It is still inefficient to find the longest incremental sub-sequence length from left to right when determining the maximum value of DP [J. if you store the longest incremental sub-sequence of the previous I-length sequence, the complexity of O (logn) can be achieved through binary search.
Use an Lis array to save the longest ascending subsequence of the first I sequence. If num [I]> Lis [Top], then Lis [++ top] = num [I]; DP [I] = top; If num [I] = Lis [Top], DP [I] = top; if num [I] <Lis [Top], locate the nearest value (k) equal to or greater than num [I] in the binary system ), DP [I] = k-1; Lis [k] = num [I];
Question: Ultraviolet A-10534 wavio Sequence
Pseudocode
DP [maxn], Lis [maxn], s [maxn]; Top = 0; Lis [top ++] = s [1]; for I in range (2, Len): If s [I]> Lis [Top]: Lis [++ top] = s [I]; DP [I] = Top + 1; else if s [I] = Lis [Top]: DP [I] = Top + 1; else: K = lower_bound (LIS. begin (), Lis. end (), s [I])-lis. beign (); // the header file Lis [k] = s [I]; DP [I] = k + 1; else:; return DP [maxn];
Longest Common subsequence O (N * logn)
The string must not contain the same number or letter. By ing the first string (incremental order), and then the second string is mapped according to the first string equivalence, the problem is converted from LCS to Lis. For example:
String 1: 2 4 3 5 6
Ing: 1 2 3 4 5
String 2: 3 2 6 8 10
Equivalent ing: 3 1 5 0 0
Question: uva10635prince and Princess
Summary of LIS (longest incrementing subsequence) and LCS (longest common subsequence)