文章相似度判断
代码
public static bool CompaStrPercent(string s1, string s2, double cent)
{
bool rtBool = false;
if (String.IsNullOrEmpty(s1) || String.IsNullOrEmpty(s2))
return false;
int[,] num = new int[s1.Length, s2.Length]; //2D array
int s1Len = s1.Length;
int s2Len = s2.Length;
//Actual algorithm
for (int i = 0; i < s1Len; i++)
{
for (int j = 0; j < s2Len; j++)
{
if (s1[i] == s2[j])
{
if (i == 0 || j == 0)
num[i, j] = 1;
else
num[i, j] = 1 + num[i - 1, j - 1];
}
else
{
if (i > 0 && j > 0)
{
num[i, j] = Math.Max(num[i - 1, j], num[i, j - 1]);
}
else if (i == 0 && j > 0) //First ith element
{
num[i, j] = num[i, j - 1];
}
else if (j == 0 && i > 0) //First jth element
{
num[i, j] = num[i - 1, j];
}
else //(i == 0 && j == 0)
{
num[i, j] = 0;
}
}
}//end j
if (num[i, s2Len - 1] > cent)
{
rtBool = true;
break;
}
if ((s1Len - 1 - i + num[i, s2Len - 1]) < cent)
{
rtBool = false;
break;
}
}//end i
return rtBool;
}