Longest common sub-sequence
1#include <stdio.h>2#include <stdlib.h>3#include <time.h>4#include <string.h>5 6 #defineN 57 8 intMaxintAintBintc) {9 Ten intAB = A>b?a:b; One returnAB > C?Ab:c; A - } - the voidSolveint*array1,int*array2,intN) { -n = n +1; - - int*pre = (int*) malloc (n * n *sizeof(int)); + int*DP = (int*) malloc (n * n *sizeof(int)); - inti; + intJ; A intA, B, C; at -Bzero ((void*) DP, n * n *sizeof(int)); -Bzero ((void*) Pre, n * n *sizeof(int)); - - for(i =1; I < n; i++) { - for(j =1; J < N; J + +) { inA = * (DP + (i-1) *n +j); -B = * (DP + i * n + J-1); toc = * (Array1 + J-1) = = * (Array2 + i-1) ? * (DP + (i-1) *n + J-1) +1: * (DP + (i-1) *n + J-1); +* (DP + i * n + j) =Max (A, B, c); - } the } * $ for(i =0; I < n; i++) {Panax Notoginseng for(j =0; J < N; J + +) { -printf"%d\t", * (DP + i * n +j)); the } +printf"\ n"); A } the + Free (DP); - Free (pre); $ } $ - intMainChar**args) { - Srand ((unsigned) time (NULL)); the - int*array1 = (int*) malloc (N *sizeof(int));Wuyi for(inti =0; i < N; i++) { the* (array1 + i) = rand ()%N; -printf"%d\t", * (Array1 +i)); Wu } -printf"\ n"); About $ int*array2 = (int*) malloc (N *sizeof(int)); - for(inti =0; i < N; i++) { -* (array2 + i) = rand ()%N; -printf"%d\t", * (Array2 +i)); A } + theprintf"\ n"); -printf"\ n"); $ the Solve (Array1, array2, N); the the Free (array1); the Free (array2); - return 0; in}
[Algorithm] dynamic programming of the longest common sub-sequence