I’m trying to solve this problem and my code is working fine for given test cases but giving wrong answer when I submit it. What should be the correct output of test case:
4 7
1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 6
zbbz
bbczcbb
My code is giving 14 as output.
Please Help me solve this question.
#include
using namespace std;
long long max(long long a, long long b){
return (a>b ? a:b);
}
int main(){
long long n, m;
cin >> n >> m;
long long cost[26];
char str1[n], str2[m];
for (long long i = 0; i < 26; ++i)
{
cin >> cost[i];
}
cin >> str1;
cin >> str2;
long long dp[n+1][m+1];
for (long long i = 0; i < n+1; ++i)
{
dp[i][0] = 0;
}
for (long long i = 0; i < m+1; ++i)
{
dp[0][i] = 0;
}
for (long long i = 1; i <= n; ++i)
{
for (long long j = 1; j <= m; ++j)
{
if(str1[i-1]==str2[j-1]){
dp[i][j] = dp[i-1][j-1] + cost[int(str1[i-1]) - 97];
}
else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
cout << dp[n][m];
}