会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
清豪的技术博客
没有太多的豪言壮语,只是想记录一些学习成长过程中的点点滴滴,可能还很初级,但每天都在成长,就够了
博客园
首页
新随笔
联系
订阅
管理
查找匹配度最高的字符串(转)
要求在3W多个中,找出与给定的字符串匹配度最高的字符串. 匹配度计算规则: 匹配度=最长的相同字串的长度 / 两个字符串中最长的字符串的长度.
要求在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);
}
}
比较规则的实现:
/**/
///
<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;
}
posted on
2010-06-24 10:44
清豪
阅读(
2107
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部