博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

计算字符串的相似度(编程之美3.3)

Posted on 2010-09-22 14:52  KurtWang  阅读(410)  评论(0编辑  收藏  举报
// 3_3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
//递归
int distance(const char * str1, int start1, int end1, const char * str2, int start2, int end2)
{
	if(start1 > end1)
	{
		if(start2 > end2)
			return 0;
		else
			return end2-start2+1;
	}
	if(start2 > end2)
	{
		if(start1 > end1)
			return 0;
		else 
			return end1-start1+1;
	}

	if(str1[start1] == str2[start2])
	{
		return distance(str1, start1+1, end1, str2, start2+1,end2);
	}
	else
	{
		int t1 = distance(str1, start1+1,end1,str2,start2+1, end2)+1;
		int t2 = distance(str1, start1,end1,str2,start2+1, end2)+1;
		int t3 = distance(str1, start1+1,end1,str2,start2, end2)+1;
		if(t1 < t2 && t1 < t3)
			return t1;
		else if(t2 < t1 && t2 < t3)
			return t2;
		else 
			return t3;
	}
}
//动态规划
int distance2(const char * str1, int len1, const char * str2, int len2)
{
	int ** dist = new int * [len1+1];
	for(int i=0;i<=len1;i++)
		dist[i] = new int[len2+1];
	for(int i=0;i<=len1;i++)
		dist[i][len2]=len1-i;
	for(int i=0;i<=len2;i++)
		dist[len1][i]=len2-i;
	for(int i=len1-1;i>=0;i--)
	{
		for(int j=len2-1;j>=0;j--)
		{
			if(str1[i] == str2[j])
			{
				dist[i][j]=dist[i+1][j+1];
			}
			else
			{
				if(dist[i+1][j] < dist[i+1][j+1] && dist[i+1][j] < dist[i][j+1])
					dist[i][j] = dist[i+1][j] + 1;
				else if(dist[i][j+1] < dist[i+1][j+1] && dist[i][j+1] < dist[i+1][j])
					dist[i][j] = dist[i][j+1] + 1;
				else
					dist[i][j] = dist[i+1][j+1] + 1;
			}
		}
	}
	return dist[0][0];
}



int _tmain(int argc, _TCHAR* argv[])
{
	char str1[] = "abcde";
	char str2[] = "bcde";
	printf("distance between %s and %s id %d\n", str1, str2, distance(str1, 0, strlen(str1)-1, str2, 0, strlen(str2)-1));
	printf("distance between %s and %s id %d\n", str1, str2, distance2(str1, strlen(str1), str2, strlen(str2)));
	return 0;
}