华为OJ(计算字符串的相似度)

本文介绍了一种计算两个字符串相似度的方法,通过定义操作距离来衡量两字符串间的差异,并提供了两种算法实现:递归方法和动态规划方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:计算字符串的相似度

描述

对于不同的字符串,我们希望能有办法判断相似程度,我们定义了一套操作方法来把两个不相同的字符串变得相同,具体的操作方法如下:

1 修改一个字符,如把“a”替换为“b”。

2 增加一个字符,如把“abdd”变为“aebdd”。

3 删除一个字符,如把“travelling”变为“traveling”。

比如,对于“abcdefg”和“abcdef”两个字符串来说,我们认为可以通过增加和减少一个“g”的方式来达到目的。上面的两种方案,都只需要一次操作。把这个操作所需要的次数定义为两个字符串的距离,而相似度等于“距离+1”的倒数。也就是说,“abcdefg”和“abcdef”的距离为1,相似度为1/2=0.5.

给定任意两个字符串,你是否能写出一个算法来计算出它们的相似度呢?

 

请实现如下接口

 /* 功能:计算字符串的相似度
  * 输入:pucAExpression/ pucBExpression:字符串格式,如: "abcdef"
  * 返回:字符串的相似度,相似度等于“距离+1”的倒数,结果请用1/字符串的形式,如1/2
  */
 public static  String  calculateStringDistance(String expressionA, String expressionB)
 {
     /* 请实现*/
     return null;
 }

 

约束:

1、PucAExpression/ PucBExpression字符串中的有效字符包括26个小写字母。

2、PucAExpression/ PucBExpression算术表达式的有效性由调用者保证;
3、超过result范围导致信息无法正确表达的,返回null。

 

 

 

知识点
运行时间限制 10M
内存限制 128
输入

输入两个字符串

输出

输出相似度,string类型

样例输入 abcdef abcdefg
样例输出 1/2
网上看到有两种方法:

1第一种是《编程之美》中基于递归的,他们说有重复的情况,我也不明白肿么重复了。

int calStringDis(string strA, int pABegin,int pAEnd,string strB, int pBBegin,int pBEnd)
{  
    if (pABegin > pAEnd)  
    {  
        if (pBBegin > pBEnd)  
            return 0;   
        else  
            return pBEnd - pBBegin + 1;  
    }  
    if (pBBegin > pBEnd)  
    {  
        if(pABegin > pAEnd)  
            return 0;  
        else  
            return pAEnd - pABegin + 1;  
    }  
    if (strA[pABegin] == strB[pBBegin])  
    {  
        return calStringDis(strA,pABegin+1,pAEnd,strB,pBBegin+1,pBEnd);  
    }  
    else  
    {  
        int t1 = calStringDis(strA,pABegin+1,pAEnd,strB,pBBegin+2,pBEnd);  
        int t2 = calStringDis(strA,pABegin+2,pAEnd,strB,pBBegin+1,pBEnd);  
        int t3 = calStringDis(strA,pABegin+2,pAEnd,strB,pBBegin+2,pBEnd);  
  
        return minValue(t1,t2,t3)+1;  
    }  
}


2第二种是动态规划,和最长公共子序列类似,感觉两种方法思路没有什么区别。

#include<iostream>
#include<string>
using namespace std;
int calculateDist(string,string);
int min_value(int,int,int);
int main()
{
	string s1,s2;
	cin>>s1;
	cin>>s2;
	cout<<"1/"<<(1+calculateDist(s1,s2))<<endl;
	//system("pause");
	return 0;
}
int calculateDist(string s1,string s2)
{
	int len1=s1.size()+1;
	int len2=s2.size()+1;
	int **cnt=new int*[len1];
	for(int i=0;i<len1;++i)
		cnt[i]=new int[len2];
	for(int i=0;i<len1;++i)
		cnt[i][0]=i;
	for(int j=0;j<len2;++j)
		cnt[0][j]=j;
	for(int i=1;i<len1;++i)
		for(int j=1;j<len2;++j)
			if(s1[i-1]==s2[j-1])
				cnt[i][j]=cnt[i-1][j-1];
			else
				cnt[i][j]=min_value(cnt[i-1][j-1],cnt[i-1][j],cnt[i][j-1])+1;
	int num=cnt[len1-1][len2-1];
	for(int i=0;i<len1;++i)
		delete [] cnt[i];
	delete [] cnt;
	return num;
}
int min_value(int a,int b,int c)
{
	int min;
	if(a<b)
		min=a;
	else
		min=b;
	if(min>c)
		min=c;
	return min;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值