#include <iostream>
#include <string>
using namespace std;
int Min(int a,int b,int c)
{
int t = a<b?a:b;
return t<c?t:c;
}
int CalDistance(const string &strA,int pAbegin,int pAend,
const string &strB,int pBbegin,int pBend)
{
if(pAbegin>pAend){
if(pBbegin>pBend)return 0;
else return pBend-pBbegin+1;
}
if(pBbegin>pBend){
if(pAbegin>pAend)return 0;
else return pAend-pAbegin+1;
}
if(strA[pAbegin] == strB[pBbegin]){
return CalDistance(strA,pAbegin+1,pAend,strB,pBbegin+1,pBend);
}
else{
int disa = CalDistance(strA,pAbegin+1,pAend,strB,pBbegin+1,pBend) + 1;//替换
int disb = CalDistance(strA,pAbegin,pAend,strB,pBbegin+1,pBend) + 1;//a插入
int disc = CalDistance(strA,pAbegin+1,pAend,strB,pBbegin,pBend) + 1;//b插入
return Min(disa,disb,disc);
}
}
int main()
{
//string strA="abc";
// string strB="abe";
string strA = "lsjd";
string strB = "ls";
cout<<CalDistance(strA,0,strA.size()-1,strB,0,strB.size()-1)<<endl;
system("pause");
return 0;
}
#include <string>
using namespace std;
int Min(int a,int b,int c)
{
int t = a<b?a:b;
return t<c?t:c;
}
int CalDistance(const string &strA,int pAbegin,int pAend,
const string &strB,int pBbegin,int pBend)
{
if(pAbegin>pAend){
if(pBbegin>pBend)return 0;
else return pBend-pBbegin+1;
}
if(pBbegin>pBend){
if(pAbegin>pAend)return 0;
else return pAend-pAbegin+1;
}
if(strA[pAbegin] == strB[pBbegin]){
return CalDistance(strA,pAbegin+1,pAend,strB,pBbegin+1,pBend);
}
else{
int disa = CalDistance(strA,pAbegin+1,pAend,strB,pBbegin+1,pBend) + 1;//替换
int disb = CalDistance(strA,pAbegin,pAend,strB,pBbegin+1,pBend) + 1;//a插入
int disc = CalDistance(strA,pAbegin+1,pAend,strB,pBbegin,pBend) + 1;//b插入
return Min(disa,disb,disc);
}
}
int main()
{
//string strA="abc";
// string strB="abe";
string strA = "lsjd";
string strB = "ls";
cout<<CalDistance(strA,0,strA.size()-1,strB,0,strB.size()-1)<<endl;
system("pause");
return 0;
}