zoukankan      html  css  js  c++  java
  • 比较两个字符串的最长的相同字符串 及其位置和百分比

      /// <summary>
            /// 
            /// </summary>
            /// <param name="DataKey"></param>
            /// <param name="DataSoure"></param>
            /// <param name="minPercentage"></param>
            /// <param name="maxPercentage"></param>
            /// <param name="minstarLoca"></param>
            /// <param name="maxstarLoca"></param>
            /// <returns></returns>
            public static string getSomeString(string DataKey
                                              ,string DataSoure
                                              ,out string minPercentage
                                              ,out string maxPercentage
                                              ,out int minstarLoca
                                              ,out int maxstarLoca) 
            {
                string strReturn = "";
                int strBeginStar = 0,strlength=1,maxlength=0;
                bool IsGoOn=true;
                string strTemp ="";
                //使用短的区匹配长的 加快速度
                string strKey, strSoure;
                strKey = DataKey.Length >= DataSoure.Length ? DataSoure : DataKey;
                strSoure = DataKey.Length > DataSoure.Length ? DataKey : DataSoure;
                minstarLoca = 0; maxstarLoca = 0; minPercentage = "0"; maxPercentage = "0";
                while (maxlength <= strKey.Length)
                 {
                    strTemp = strKey.Substring(strBeginStar, strlength);
                    while (strSoure.IndexOf(strTemp) > -1 && maxlength <= strKey.Length)
                    {
                        minstarLoca = strBeginStar;
                        maxstarLoca = strSoure.IndexOf(strTemp);
                        strReturn = strTemp;
                        strlength = strlength + 1;
                        maxlength = strlength + strBeginStar;
                        if (maxlength <= strKey.Length)
                        {
                            strTemp = strKey.Substring(strBeginStar, strlength);
                        }
                    }
                    strBeginStar = strBeginStar + 1;
                    maxlength = strlength + strBeginStar;     
                 }
                minPercentage = ((double)strReturn.Length * 100 / strSoure.Length).ToString();
                maxPercentage = ((double)strReturn.Length * 100 / strKey.Length).ToString();
                return strReturn;
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                string strMax,strMin;
                int minstar,maxstar;
                string str = getSomeString(TextBox1.Text.Trim(), TextBox2.Text.Trim(), out strMin, out strMax, out minstar, out maxstar);
                Label1.Text = "最大百分比为:" + strMax + "最小百分比为:" + strMin + "在小字符串中开始位置:" + minstar.ToString() + "在大字符串中开始位置:" + maxstar.ToString()+ "<br/>匹配字符串为:" + str;
            }

     
  • 相关阅读:
    SQLServer 可疑
    String与Long互转
    洛谷 P5644
    洛谷 P3783
    洛谷 P4663
    洛谷 P3438
    Atcoder Grand Contest 054 题解
    迭代器失效问题
    Solution -「CF 232E」Quick Tortoise
    Solution -「NOI 2020」「洛谷 P6776」超现实树
  • 原文地址:https://www.cnblogs.com/dingdingmao/p/3146528.html
Copyright © 2011-2022 走看看