惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - phcis

c# 全角(SBC)和半角(DBC)相互转换函数 用HttpWebRequest做POST请求时返回Http 417 错误解决方法 使用webbroswer的一点技巧记录 asp.net中js返回false时阻止form提交的方法 【转】DIV+CSS 让文字居中于背景图 sql 语句实现分页 【转】C#产生随机字符的两段代码 【转】datagridview的checkbox列,当修改checkbox状态时实时获得其准确状态值 c# 读取excel的一系列问题 - phcis - 博客园 由于使用“优易U盘加密软件”导致电脑无法关机/蓝屏等解决方法 批量删除数据库中所有表的记录(清空数据库) C# HttpRequest基础连接已经关闭: 接收时发生意外错误 GridView 动态绑定数据,包括2个或者多个值 - phcis - 博客园 近期动向 使用ajax导致滚动条复位的解决方法 AT编程常见问题与错误代码的意义 c# 发送email,正文支持html格式,包含附件 使用AutocompleteExtender无效或者没反应的原因记录 使用 DateTimePicker 控件显示和选择时间
计算字符串相似度及寻找最相似字符串的代码
phcis · 2011-06-30 · via 博客园 - phcis

计算两个字符串的差异距离函数

/// <summary>
/// 计算两个字符串的差异距离
/// </summary>
/// <param name="source">来源字符串</param>
/// <param name="target">目标字符串</param>
/// <returns>字符串差距</returns>
public int CalcDistance(string source, string target)
{
int n = source.Length;
int m = target.Length;
if (m == 0) return n;
if (n == 0) return m;
var matrix
= new int[n + 1, m + 1];
for (int i = 1; i <= n; i++)
{
matrix[i,
0] = i;
}
for (int i = 1; i <= m; i++)
{
matrix[
0, i] = i;
}
for (int i = 1; i <= n; i++)
{
var si
= source[i - 1];
for (int j = 1; j <= m; j++)
{
var tj
= target[j - 1];int cost;
if (si == tj)
cost
= 0;
else
cost
= 1;int above = matrix[i - 1, j] + 1;
int left = matrix[i, j - 1] + 1;
int diag = matrix[i - 1, j - 1] + cost;
matrix[i, j]
= Math.Min(above, Math.Min(left, diag));
}
}
return matrix[n, m];
}

计算两个字符串的相似度  

/// <summary>
/// 计算两个字符串的相似度
/// </summary>
/// <param name="source">来源字符串</param>
/// <param name="target">目标字符串</param>
/// <returns>相似度</returns>
public double CalcSimilarity(string source, string target)
{
int n = source.Length;
int m = target.Length;
if (n == 0 || m == 0)
return 0;
int distance = CalcDistance(source, target);
int max = Math.Max(n, m);
return 1.0 * (max - distance) / max;
}

demo:

List<string> lvArea = new List<string>();
lvArea.Add(
"北京电信一区");
lvArea.Add(
"广东一区");
lvArea.Add(
"北京网通二区");
lvArea.Add(
"北京网通一区");decimal lvSmall = 100;
string lvRes = "";
foreach (string item in lvArea)
{
decimal lvTemp = CalcDistance("北京网通二区", item);
if (lvTemp < lvSmall)
{
lvSmall
= lvTemp;
lvRes
= item;
}

}
MessageBox.Show(lvRes);