Question: Ultraviolet A-531 compromise (LIS)
Give two paragraphs to find out the subsequence of the longest common word in it. And output any subsequence.
Solution: Lis.
Code:
# Include <cstdio> # include <cstring> const int n = 105; const int M = 35; char W1 [N] [m]; char W2 [N] [m]; int f [N] [N]; int P [N] [N] [2]; int ans [N]; int N1, N2; void printf_ans (int x, int y) {If (x = 0 | Y = 0) return; printf_ans (P [x] [y] [0], P [x] [y] [1]); If (strcmp (W1 [X-1], W2 [Y-1]) = 0) ans [f [x] [Y] = x-1;} int main () {n1 = n2 = 0; while (scanf ("% s ", w1 [N1 ++])! = EOF) {While (scanf ("% s", W1 [N1]) & W1 [N1] [0]! = '#') {N1 ++;} while (scanf ("% s", W2 [n2]) & W2 [n2] [0]! = '#') {N2 ++;} For (INT I = 0; I <= N1; I ++) f [I] [0] = 0; for (INT I = 0; I <= n2; I ++) f [0] [I] = 0; For (INT I = 1; I <= N1; I ++) for (Int J = 1; j <= n2; j ++) {If (strcmp (W1 [I-1], W2 [J-1]) = 0) {f [I] [J] = f [I-1] [J-1] + 1; P [I] [J] [0] = I-1; p [I] [J] [1] = J-1 ;} else {If (F [I] [J-1]> F [I-1] [J]) {f [I] [J] = f [I] [J-1]; P [I] [J] [0] = I; P [I] [J] [1] = J-1;} else {f [I] [J] = f [I-1] [J]; P [I] [J] [0] = I-1; p [I] [J] [1] = J ;}} printf_ans (N1, N2 ); for (INT I = 1; I <F [N1] [n2]; I ++) printf ("% s", W1 [ans [I]); printf ("% s \ n", W1 [ans [f [N1] [n2]); n1 = n2 = 0 ;}}
Ultraviolet-531 compromise (LIS)