//============================================================================
// Name : reversestring.cpp
// Author : qxyu
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<unordered_map>
#include<memory>
//#include<uint32_t>
using namespace std;
/**
* Definition for a point.
*/
int edit_dis(string& a, string& b){
int ed[51][51];
for(int i = 0;i< a.size(); i ++)
ed[i][0]=i;
for(int i = 0;i< b.size(); i ++)
ed[0][i]=i;
int tmp;
int d;
int res;
for(int i = 0; i < a.size(); i ++){
for(int j = 0; j < b.size(); j ++){
tmp=min(ed[i-1][j],ed[i][j-1]);
if(a[i]==b[j]) d = 0;
else d = 1;
ed[i][j]=min(ed[i-1][j-1]+d,tmp+1);
res=ed[i][j];
}
}
return res;
}
int main()
{
string a = "ABCBDAB";
string b = "BDCABA";
cout<<edit_dis(a,b);
return 0;
}
最短编辑距离
最新推荐文章于 2024-05-04 14:23:42 发布