当前位置:编程学习 > C#/ASP.NET >>

C# 求字符串中出现次数最多的子串

求一个字符串中出现最多的(长度大于1的)子串。如对于“abcsadbabdkabfab”返回“ab”
用算法怎么写? --------------------编程问答-------------------- --------------------编程问答-------------------- 这种基本问题可以很好地区别你适合不适合写程序。 --------------------编程问答-------------------- 只说思路,ascii码判断,for循环
只要代码而不动手的,代码永远不是你的。在这里不是说教,我也不过是刚毕业刚工作的。 --------------------编程问答-------------------- 如果abcabc呢,是返回一个就可以还是要返回全部? --------------------编程问答-------------------- 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]);
                        }
                    }

                } 

--------------------编程问答-------------------- 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";
            } --------------------编程问答-------------------- 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_计数;
        }

这个很挫的方式。 --------------------编程问答-------------------- 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;
                }
            }
        } --------------------编程问答-------------------- 求指定子串在字符串中出现的次数

Regex.Matches("abcsadbabdkabfab", substr).Count; --------------------编程问答-------------------- 例如
Regex.Matches("abcsadbabdkabfab", "ab").Count;
的值为4

把所有子串得到的出现次数进行比较,得出最大值即可
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,