要求在3W多个中,找出与给定的字符串匹配度最高的字符串.
匹配度计算规则:
匹配度=最长的相同字串的长度 / 两个字符串中最长的字符串的长度.
代码如下:
定义的一个主体类:
public delegate decimal CompareCondition(String value);
public class StringCompare
{
private List<IntraString> intraList = new List<IntraString>();
public StringCompare(List<String> strlist)
{
for (int i = 0; i < strlist.Count; i++)
{
intraList.Add(new IntraString(strlist[i]));
}
}
public List<IntraString> Compare(CompareCondition condition)
{
for (int i = 0; i < intraList.Count; i++)
{
intraList[i].Compare(condition);
}
intraList.Sort(delegate(IntraString a, IntraString b)
{
if (a.CompareResult > b.CompareResult)
{
return 1;
}
else if (a.CompareResult == b.CompareResult)
{
return 0;
}
else
{
return -1;
}
}
);
return intraList;
}
}
public class IntraString
{
private String value;
public IntraString(String value)
{
this.value = value;
}
public String Value
{
get { return value; }
set { this.value = value; }
}
private decimal compareResult;
public decimal CompareResult
{
get { return compareResult; }
set { compareResult = value; }
}
public void Compare(CompareCondition condition)
{
CompareResult = condition(Value);
}
}
public class StringCompare
{
private List<IntraString> intraList = new List<IntraString>();
public StringCompare(List<String> strlist)
{
for (int i = 0; i < strlist.Count; i++)
{
intraList.Add(new IntraString(strlist[i]));
}
}
public List<IntraString> Compare(CompareCondition condition)
{
for (int i = 0; i < intraList.Count; i++)
{
intraList[i].Compare(condition);
}
intraList.Sort(delegate(IntraString a, IntraString b)
{
if (a.CompareResult > b.CompareResult)
{
return 1;
}
else if (a.CompareResult == b.CompareResult)
{
return 0;
}
else
{
return -1;
}
}
);
return intraList;
}
}
public class IntraString
{
private String value;
public IntraString(String value)
{
this.value = value;
}
public String Value
{
get { return value; }
set { this.value = value; }
}
private decimal compareResult;
public decimal CompareResult
{
get { return compareResult; }
set { compareResult = value; }
}
public void Compare(CompareCondition condition)
{
CompareResult = condition(Value);
}
}
比较规则的实现:
/// <summary>
///定义比较的条件
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static decimal Compare(String value)
{
string str = Guid.NewGuid().ToString().Replace("-", "");
int MaxLength = str.Length;
if (str.Length < value.Length)
{
MaxLength = value.Length;
}
bool find = false;
decimal sameCount = 0;
for (int i = 0; i < value.Length; i++)
{
string newStr = value.Substring(i, value.Length - i);
for (int j = newStr.Length - 1; j >= 0; j--)
{
if (str.IndexOf(newStr.Substring(0,j+1)) >= 0)
{
newStr = newStr.Substring(0, j + 1);
find = true;
break;
}
}
if (find)
{
sameCount = newStr.Length;
break;
}
}
return sameCount / MaxLength;
}
///定义比较的条件
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static decimal Compare(String value)
{
string str = Guid.NewGuid().ToString().Replace("-", "");
int MaxLength = str.Length;
if (str.Length < value.Length)
{
MaxLength = value.Length;
}
bool find = false;
decimal sameCount = 0;
for (int i = 0; i < value.Length; i++)
{
string newStr = value.Substring(i, value.Length - i);
for (int j = newStr.Length - 1; j >= 0; j--)
{
if (str.IndexOf(newStr.Substring(0,j+1)) >= 0)
{
newStr = newStr.Substring(0, j + 1);
find = true;
break;
}
}
if (find)
{
sameCount = newStr.Length;
break;
}
}
return sameCount / MaxLength;
}