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

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos 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);