#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define MIN(x, y, z) ((x < y) ? (x < z ? x : z) : (y < z ? y : z))
int calcStringDistance(const string &s1, int beg1, int end1, const string &s2, int beg2, int end2)
{
if(beg1 > end1)
{
if(beg2 > end2)
return 0;
else
return (end2-beg2+1);
}
if(beg2 > end2)
{
if(beg1 > end1)
return 0;
else
return (end1-beg1+1);
}
if(s1[beg1] == s2[beg2])
calcStringDistance(s1, beg1+1, end1, s2, beg2+1, end2);
else
{
int d1 = calcStringDistance(s1, beg1+1, end1, s2, beg2, end2);
int d2 = calcStringDistance(s1, beg1, end1, s2, beg2+1, end2);
int d3 = calcStringDistance(s1, beg1+1, end1, s2, beg2+1, end2);
return MIN(d1, d2, d3)+1;
}
}
int main()
{
string s1 = "123450";
string s2 = "012345";
int distance = calcStringDistance(s1, 0, s1.size()-1, s2, 0, s2.size()-1);
cout << distance <<endl;
}
calculate the distance of two string
最新推荐文章于 2013-08-07 14:12:31 发布