资源简介
通过编辑距离算法对两字符串相似度对比后顺序取出所有公共子串
代码片段和文件信息
/**
* 名称:行鹏程
* 描述: 编辑距离算法
* 创建人: 行鹏程
* 版本: 1.0
* 创建时间: 2017年06月01日 上午11:17:05
*/
public class LevenshteinDistance
{
private static LevenshteinDistance _instance = null;
public static LevenshteinDistance Instance
{
get
{
if (_instance == null)
{
return new LevenshteinDistance();
}
return _instance;
}
}
///
/// 取最小的一位数
///
///
///
///
///
public int LowerOfThree(int first int second int third)
{
int min = first;
if (second < min)
min = second;
if (third < min)
min = third;
return min;
}
///
/// 编辑距离算法
///
/// 待比较数据
/// 待比较数据
///
public int Levenshtein_Distance(string str1 string str2)
{
int[] Matrix;
int n=str1.Length;
int m=str2.Length;
int temp = 0;
char ch1;
char ch2;
int i = 0;
int j = 0;
if (n ==0)
{
return m;
}
if (m == 0)
{
return n;
}
Matrix=new int[n+1m+1];
for (i = 0; i <= n; i++)
{
//初始化第一列
Matrix[i0] = i;
}
for (j = 0; j <= m; j++)
{
//初始化第一行
Matrix[0 j] = j;
}
for (i = 1; i <= n; i++)
{
ch1 = str1[i-1];
for (j = 1; j <= m; j++)
{
ch2 = str2[j-1];
if (ch1.Equals(ch2))
{
temp = 0;
}
else
{
temp = 1;
}
Matrix[ij] = LowerOfThree(Matrix[i - 1j] + 1 Matrix[ij - 1] + 1 Matrix[i - 1j - 1] + temp);
}
}
//for (i = 0; i <= n; i++)
//{
// for (j = 0; j <= m; j++)
// {
// Console.Write(“ {0} “ Matrix[i j]);
// }
// Console.WriteLine(““);
//}
return Matrix[n m];
}
///
/// 计算字符串相似度
///
///
///
///
public decimal LevenshteinDistancePercent(string str1string str2)
{
int maxLenth = str1.Length > str2.Length ? str1.Length : str2.Length;
int val = Levenshtein_Distance(str1 str2);
return 1 - (decimal)val / maxLenth;
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-01-18 12:48 TestCompareStr\
目录 0 2019-01-21 09:35 TestCompareStr\TestCompareStr\
文件 2411 2019-01-18 12:52 TestCompareStr\TestCompareStr\LevenshteinDistance.cs
文件 3028 2019-01-21 09:34 TestCompareStr\TestCompareStr\Proc.cs
文件 1648 2019-01-21 09:35 TestCompareStr\TestCompareStr\Program.cs
目录 0 2019-01-18 12:48 TestCompareStr\TestCompareStr\Properties\
文件 1348 2019-01-18 12:48 TestCompareStr\TestCompareStr\Properties\AssemblyInfo.cs
文件 2666 2019-01-20 10:44 TestCompareStr\TestCompareStr\TestCompareStr.csproj
目录 0 2019-01-21 09:35 TestCompareStr\TestCompareStr\bin\
目录 0 2019-01-18 12:59 TestCompareStr\TestCompareStr\bin\Debug\
文件 17920 2019-01-21 09:17 TestCompareStr\TestCompareStr\bin\Debug\TestCompareStr.exe
文件 46592 2019-01-21 09:17 TestCompareStr\TestCompareStr\bin\Debug\TestCompareStr.pdb
文件 22472 2019-01-21 09:18 TestCompareStr\TestCompareStr\bin\Debug\TestCompareStr.vshost.exe
文件 490 2014-01-13 21:31 TestCompareStr\TestCompareStr\bin\Debug\TestCompareStr.vshost.exe.manifest
目录 0 2019-01-21 09:36 TestCompareStr\TestCompareStr\bin\Release\
目录 0 2019-01-18 12:48 TestCompareStr\TestCompareStr\obj\
目录 0 2019-01-21 09:17 TestCompareStr\TestCompareStr\obj\Debug\
文件 6505 2019-01-21 09:35 TestCompareStr\TestCompareStr\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
目录 0 2019-01-21 09:36 TestCompareStr\TestCompareStr\obj\Debug\TempPE\
文件 638 2019-01-21 09:18 TestCompareStr\TestCompareStr\obj\Debug\TestCompareStr.csproj.FileListAbsolute.txt
文件 1753 2019-01-18 12:57 TestCompareStr\TestCompareStr\obj\Debug\TestCompareStr.csprojResolveAssemblyReference.cache
文件 17920 2019-01-21 09:17 TestCompareStr\TestCompareStr\obj\Debug\TestCompareStr.exe
文件 46592 2019-01-21 09:17 TestCompareStr\TestCompareStr\obj\Debug\TestCompareStr.pdb
文件 932 2019-01-18 12:48 TestCompareStr\TestCompareStr.sln
文件 50688 2019-01-20 18:16 TestCompareStr\TestCompareStr.v11.suo
- 上一篇:STM32F103用IAR编译的工程模板
- 下一篇:excle打开nc文件插件
评论
共有 条评论