题目:《编程之美》P223
string sub_string(const string &s, const int begin,const int end) { if (begin > end) return ""; return s.substr(begin, end); } int strings_distance(string s1, string s2) { if (s1.size() < s2.size()) { string tmp = s1; s1 = s2; s2 = tmp; } if (s2.size() == 0) return s1.size(); int i; for (i = 0; i < s2.size(); i++) { if (s1[i] != s2[i]) break; } if (i == s2.size()) return s1.size() - s2.size(); if (i != 0) { s1 = sub_string(s1, i, s1.size() - 1); s2 = sub_string(s2, i, s2.size() - 1); return strings_distance(s1, s2); } int r1 = strings_distance(sub_string(s1, 1, s1.size() - 1), s2); int r2 = strings_distance(sub_string(s2, 1, s2.size() - 1), s1); int r3 = strings_distance(sub_string(s1, 1, s1.size() - 1), sub_string(s2, 1, s2.size() - 1)); r1 = r1>r2 ? r2 : r1; r1 = r1 > r3 ? r3 : r1; return r1 + 1; }
原文:http://blog.csdn.net/bupt8846/article/details/43453899