A Collection of Dynamic Programming Interview Questions Solved in C++ by Antonio Gulli

A Collection of Dynamic Programming Interview Questions Solved in C++ by Antonio Gulli

Author:Antonio Gulli [Gulli, Antonio]
Language: eng
Format: epub
Published: 2014-05-22T00:00:00+00:00


Code

int LCSubStr(const std::string & S1, const std::string & S2)

{

unsigned int m = S1.length();

unsigned int n = S2.length();

unsigned int result = 0;

Matrix<unsigned int> LCSuff(m + 1, n + 1);

for (unsigned int i = 0; i <= m; i++) {

for (unsigned int j = 0; j <= n; j++) {

if (i == 0 || j == 0) {

LCSuff(i, j) = 0;

} else if (S1[i - 1] == S2[j - 1]) {

result = std::max(

(LCSuff(i, j) = LCSuff(i - 1, j - 1) + 1),

result

);

} else {

LCSuff(i, j) = 0;

}

}

}

return result;

}

Complexity

Complexity is and space is. Reducing the space to is left as an exercise.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.