求一个字符串中出现最多的(长度大于1的)子串。如对于“abcsadbabdkabfab”返回“ab”
用算法怎么写?

解决方案 »

  1.   

    只说思路,ascii码判断,for循环
    只要代码而不动手的,代码永远不是你的。在这里不是说教,我也不过是刚毕业刚工作的。
      

  2.   

    如果abcabc呢,是返回一个就可以还是要返回全部?
      

  3.   

    1.枚举求所有子串
    2.指定子串在字符串中出现的次数---书中和网上很多1.
                    for (int i = 2; i < str.Length; i++)//i总个数
                    {
                        for (int j = 0; j < str.Length - i + 1; j++)//j开始位置
                        {
                            for (int k = 0; k < i; k++)//k个数总是小于i
                            {
                                //eg:str1xin += Convert.ToString(str[j + k]);
                            }
                        }                } 
      

  4.   

    int len = srcStr.Length;
                Dictionary<string, int> results = new Dictionary<string, int>();
                for (int i = 2; i <= len; i++ )
                {
                    //calculate the string with largest frequency the length of which is i
                    //key: string
                    //value: frequency
                    
                    int j=0;
                    do
                    {
                        string subStr = srcStr.Substring(j, i);
                        j++;
                        if(results.ContainsKey(subStr))
                        {
                            results[subStr] ++;
                        }
                        else
                        {
                            results.Add(subStr, 1);
                        }
                    } while (j < len-i+1);
                }            String output = "";
                foreach (KeyValuePair<string, int> item in results )
                {
                    output += item.Key+"\t"+item.Value+"\n";
                }
      

  5.   

    1.获取子串 小算法                for (int i = 2; i < str.Length; i++)//i子串个数
                    {
                        for (int j = 0; j < str.Length - i + 1; j++)//j开始位置
                        {
                            string strnew = str.Substring(j,i);
                        }
                    }2.出现次数         private int e_计数 = 0;
            private int Str_Count(string subStr, string str)
            {
                int i=0,j = 0;
                while(j<subStr.Length&&i<str.Length)
                {
                    if (str[i] == subStr[j])
                    {
                        i++;
                        j++;
                    }
                    else
                    {
                        i = i - j + 1;
                        j = 0;
                    }
                }
                if (j >= subStr.Length)
                    return e_计数++;
                else
                    return e_计数;
            }这个很挫的方式。
      

  6.   

    static void MostCommonSubString(string s, out int repeatTime, out string mostCommonString)
            {
                Dictionary<string, int> dic = new Dictionary<string, int>();
                for (int j = 2; j < s.Length; j++)
                {
                    for (int i = 0; i < s.Length - j + 1; i++)
                    {
                        string temp = s.Substring(i, j);
                        if (!dic.Keys.Contains<string>(temp))
                        {
                            dic.Add(temp, 1);
                        }
                        else
                        {
                            dic[temp] = dic[temp] + 1;
                        }
                    }
                }            
    repeatTime = 0;
                mostCommonString = string.Empty;
                foreach (string c in dic.Keys)
                {
                    if (dic[c] > repeatTime)
                    {
                        repeatTime = dic[c];
                        mostCommonString = c;
                    }
                }
            }
      

  7.   

    求指定子串在字符串中出现的次数Regex.Matches("abcsadbabdkabfab", substr).Count;
      

  8.   

    例如
    Regex.Matches("abcsadbabdkabfab", "ab").Count;
    的值为4把所有子串得到的出现次数进行比较,得出最大值即可