码迷,mamicode.com
首页 > 编程语言 > 详细

【难】【数组】计算字符的相似度

时间:2015-02-03 23:07:31      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:c++   程序员   面试   string   

题目:《编程之美》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;
}




【难】【数组】计算字符的相似度

标签:c++   程序员   面试   string   

原文地址:http://blog.csdn.net/bupt8846/article/details/43453899

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!