Why my code is not working

#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int dp[1001][1001],i,j;
string s,str,final=" ";
cin>>s>>str;
int l=s.size();
int l1=str.size();
for(i=0;i<=l;++i){
for(j=0;j<=l1;j++){
if(i==0||j==0)
dp[i][j]=0;

        else if(s[i-1]==str[j-1])
        dp[i][j]=1+dp[i-1][j-1];
        else
        dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    }
}
/*for(i=0;i<=l;i++){
    for(j=0;j<=l1;j++)
    cout<<dp[i][j]<<" ";
    cout<<endl;
}*/
i=l1;j=l;
int index=dp[l1][l];
while(i>0&&j>0){
    if(s[i-1]==str[j-1]){
		//cout<<s[i-1];
        final+=s[i-1];
        i--;
       // index--;
        j--;
    }
    else if(dp[i-1][j]>dp[i][j-1])
    i--;
    else
	j--;
    
    
}
reverse(final.begin(),final.end());
cout<<final;
return 0;

}

@arjundkl Before the while loop for print, you have initialized i and j opposite, it should be i=l and j=l1.
Also dont use final as name for string because it is a existing keyword in c++.