I am trying to implement longest common subsequence problem iteratively and recursively. But my code keeps giving me Error Code -1073741819 (0xC0000005). I have tried to dry run it and the logic seems correct but I am not getting the desired results. Here is what I implemented:-
/* Dynamic Programming Solution */
#include <iostream>
#include <string>
#include <vector>
using namespace std ;
void input();
void lcs(string s1,string s2,unsigned int m ,unsigned int n);
void input(){
string s1 , s2 ;
unsigned int m , n ;
cin >> s1 ;
cout << flush ;
cin >> s2 ;
cout << flush ;
m = s1.length();
n = s2.length();
lcs(s1,s2,m,n);
// to main
}
// Tabulated function
void lcs(string s1,string s2,unsigned int m ,unsigned int n){
vector <vector<int>> memo(m + 1);
vector <int> v(n + 1);
for(int i = 0 ; i <= m ; i++ ){
memo.push_back(v);
}
for(int i = 0 ; i <= n ; i++ ){
memo[0].push_back(0) ;
}
for(int i = 0 ; i < m ; i++ ) {
memo[i].push_back(0);
}
for(int i = 1 ; i <= m ; i++ ){
for(int j = 1 ; j <= n ; j++ ){
if(s1[i - 1] == s2[j - 1]){
memo[i][j] = 1 + memo[i - 1][j - 1];
}else{
memo[i][j] = max(memo[i - 1][j],memo[i][j - 1]);
}
}
}
cout << memo[m][n];
}
int main(){
input();
return 0 ;
}