lcs is longest common subsequence algorithm lcs algorithm in c brute-force approach / input: array a of x/ / input: array b of x/ / output: pointer to beginning of longest sequence/ x * c[]; int a_counter; int b_counter; int c_counter; exists bool; int longest_sequence; int longest_sequence_index; / generate array of unique elements from a called c/ for (a_counter = a_start; a_counter < size_of_a; a_counter++) { c_counter = c_start; while ( c_counter < size_of_c ) { if (c[c_counter] == a[a_counter] ) { exists = true; break; } c_counter++; } if (exists) continue; else c[a_counter++] = a[a_counter]; exists = false; } / generate max_c array to hold max entries of c array/ /* (could also implement d as linked list)*/ int max_c[size_of_c]; for ( c_counter = c_start; c_counter < size_of_c; c_counter++) { max_c[c_counter] = 0; } / compare a[i] with ending elements of each c[0 ... size_of_c].final/ / elements/ for (b_counter = b_start+1; b_counter <= size_of_b; b_counter++) { for (c_counter = c_start; c_counter < size_of_c; c_counter++) { if (c[c_counter][max_c[c_counter]] == b[b_counter-1]) { c[c_counter][max_c[c_counter]] = b[b_counter]; max_c[c_counter]++; } } } /* find longest sequence */ int longest_sequence; for (c_counter = c_start; c_counter < size_of_c; c_counter++) { if (longest_sequence < max_c[c_counter]) { longest_sequence = max_c[c_counter]; longest_sequence_index = c_counter; } } return c[longest_sequence_index];