首页 > 编程语言 > 详细

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

时间:2015-02-03 23:06:14      阅读:390      评论:0      收藏:0      [点我收藏+]

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!