hello guys i'm new with dynamic programming this is my code for this problem and i can't understand the reason of having WA
#include <bits/stdc++.h>
using namespace std;
int memory[1002][1002];
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
for(int i=1;i<=s1.length();i++)
{
for(int j=1;j<=s2.length();j++)
{
if (s1[i-1] == s2[j-1])
memory[i][j]=memory[i-1][j-1]+1;
else
memory[i][j]=max(memory[i-1][j],memory[i][j-1]);
}
}
cout<<memory[s1.length()][s2.length()]<<'\n';
}
return 0;
}
any tips ? thanks in advance
Set all array to zero after every test.
As the 0-th row and 0-th column is always 0 so he don't need to reset the whole array to 0.
The problem nowhere says the string will not contain white space character. SO just change —
to
and get AC :)