375. 字符串的距離
★☆ 輸入文件:blast.in
輸出文件:blast.out
簡單對比時間限制:1 s 內存限制:128 MB
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX_N 3000
char str1[MAX_N];
char str2[MAX_N];
int k;
int dp[MAX_N][MAX_N];
int main()
{
freopen("blast.in","r",stdin);
freopen("blast.out","w",stdout);
scanf("%s%s%d",str1,str2,&k);
int len1=strlen(str1);
int len2=strlen(str2);
int len=max(len1,len2);
for(int i=1;i<=len;i++)
{
dp[i][0]=dp[i-1][0]+k;
dp[0][i]=dp[0][i-1]+k;
}
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++)
{
if(str1[i-1]!=str2[j-1])
{
dp[i][j]=min(dp[i-1][j]+k,dp[i][j-1]+k);
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+abs(str1[i-1]-str2[j-1]));
}
else
{
dp[i][j]=dp[i-1][j-1];
}
}
printf("%d\n",dp[len1][len2]);
return 0;
}