calculate the distance of two string

来源:互联网 发布:php 框架 编辑:程序博客网 时间:2024/04/27 16:15
#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;elsereturn (end2-beg2+1);}if(beg2 > end2){if(beg1 > end1)return 0;elsereturn (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;}