dp字符串
作者:
巷港
,
2022-03-31 09:49:22
,
所有人可见
,
阅读 137
最优包含
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010;
char a[N],b[N];
int f[N][N];
int main()
{
scanf("%s%s",a+1,b+1);
memset(f,0x3f,sizeof f);
f[0][0]=0;
int len1=strlen(a+1),len2=strlen(b+1);
for (int i=1;i<=len1;i++) f[i][0]=0;
for (int i=1;i<=len1;i++)
{
for (int j=1;j<=len2;j++)
{
f[i][j]=f[i-1][j];
if (a[i]==b[j]) f[i][j]=min(f[i][j],f[i-1][j-1]);
else f[i][j]=min(f[i][j],f[i-1][j-1]+1);
}
}
printf("%d\n",f[len1][len2]);
return 0;
}