C# 采用动态规划算法,计算两个字符串之间的相似程度。
public static double CountTextSimilarity(string textX, string textY, bool isCase = false) // 计算文本相似度
{
if (textX.Length <= 0 || textY.Length <= 0)
{
return (0);
}
if (!isCase)
{
textX = textX.ToLower();
textY = textY.ToLower();
}
int[,] dp = new int[Math.Max(textX.Length, textY.Length) + 1, Math.Max(textX.Length, textY.Length) + 1];
for (int x = 0; x < textX.Length; x++)
{
for (int y = 0; y < textY.Length; y++)
{
if (textX[x] == textY[y])
{
dp[x + 1, y + 1] = dp[x, y] + 1;
}
else
{
dp[x + 1, y + 1] = Math.Max(dp[x, y + 1], dp[x + 1, y]);
}
}
}
return (Math.Round(((double)(dp[textX.Length, textY.Length]) / Math.Max(textX.Length, textY.Length)) * 100, 2));
}
知乎: @张赐荣
赐荣博客: www.prc.cx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!