#include <iostream>
using namespace std;
char t[100];
char s[100];
int dp[100][100];
int n,m;
int DES1(){
int a,b;
for(int i=1;i<=n;i++)
dp[0][i] = i;
for(int j=1;j<=m;j++)
dp[j][0] = j;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a = min(dp[i-1][j]+1,dp[i][j-1]+1);
if(t[i] == s[j]) b = dp[i-1][j-1];
else b = dp[i-1][j-1] + 1;
dp[i][j] = min(a,b);
}
}
return dp[n][m];
}
int d(){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(t[i]==s[j]) dp[i][j] = dp[i-1][j-1];
else {
int cost = t[i] == s[j] ? 0 : 1;
int ins=dp[i][j-1]+1;
int del=dp[i-1][j]+1;
int rep = dp[i-1][j-1] +cost;
dp[i][j] = min(min(ins,del),rep);
}
}
cout<<dp[n-1][m-1];
}
int EDS(int a,int b)
{
int dis;
if(dp[a][b]) return dp[a][b];
if(n==0) dis = m;
else if(m==0) dis = n;
else if(t[a] == s[b]) EDS(a-1,b-1);
else {
int cost = t[a] == s[b] ? 0 : 1;
int ins=EDS(a,b-1)+1;
int del=EDS(a-1,b)+1;
int rep = EDS(a-1,b-1)+cost;
dis = min(min(ins,del),rep);
}
return dp[a][b] = dis;
}
int main()
{
while(cin>>n>>m){
for(int i=1;i<=n;i++)
cin>>t[i];
for(int i=1;i<=m;i++)
cin>>s[i];
cout<<DES1();
}
return 0;
}
最短编辑距离
最新推荐文章于 2023-09-20 10:13:49 发布