/* Recursively consider increasing subsequences, checking if the new character is shared by both strings and incrementing the length if necessary. */ int Program2::lengthLCS(int xIndex, int yIndex) { if (X[xIndex] == '\0' || Y[yIndex] == '\0') { return 0; // Reached the end of either string } else if (X[xIndex] == Y[yIndex]) { return (1 + lengthLCS(xIndex + 1, yIndex + 1)); } else { return (getMax(lengthLCS(xIndex + 1, yIndex), lengthLCS(xIndex, yIndex + 1))); } }
int main(int argc, char* argv[]) { FILE* infile; if(argc > 2 || argc < 2) { usage(); } infile = fopen(argv[1], "r"); if(!infile) { fprintf(stderr, "File didn't open right: %s", argv[1]); exit(0); } fgets(bufa, MAX_STRING, infile); fgets(bufb, MAX_STRING, infile); fclose(infile); printf("The LCS is %d.\n", lengthLCS()); }