随笔 - 21  文章 - 0 评论 - 6 阅读 - 42118
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

要求在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   清豪  阅读(2118)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示