.net算法题

 有如下输入:若干个由大写英文字母构成的单词,以’,’号隔开。如“ABCCD,CDA,BCDD,DCA,ADC,BCD,CDCAB”。 写一段程序,从输入中寻找由相同字符构成(重复字符出现次数也是一样)的单词组输出。
如以上输入,则输出:
第1组:ABCCD,CDCAB

第2组:CDA,DCA,ADC

本题写了两种方法,一种是统计每个字母的出现次数;另一种是把字符串分拆为string数组,在把每个string转化为char数组;通过冒泡排序后再转化为新的string,比较string就可以了。

方法二:

     static void Main(string[] args)
        {
            string input= "ABCCD,CDA,BCDD,DCA,ADC,BCD,CDCAB";              
            string[] array = input.Split(',');
            List<int> Over = new List<int>();
            Dictionary<int, int> Answer = new Dictionary<int, int>();           
            string[] newarray=new string [array.Length];
           // array.CopyTo(newarray,0);
            for (int i = 0; i < array.Length;i++ )
            {
                char[] cc = array[i].ToCharArray();
                for (int m = 0; m < cc.Length; m++)//冒泡对字符串内部进行排序
                {
                    for (int n = m + 1; n < cc.Length; n++)
                    {
                        if (cc[m] > cc[n])
                        {
                            char a = cc[n];
                            cc[n] = cc[m];
                            cc[m] = a;
                        }
                    }
                }                 
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(cc);
                newarray[i] = sb.ToString();//排序后转换为string
            }
            for (int i = 0; i < newarray.Length; i++)
            {

  if (Over.Contains(i))//已经被后面处理过了就直接跳过。
                       continue;
                int seam = 0;
                for (int j = i + 1; j < newarray.Length; j++)
                {
                  
                    if (string.Equals(newarray[i],newarray[j]))//字符串相等
                    {
                        Over.Add(j);//添加到已判断数组
                        seam++;
                    }
                }
                  if (seam>0)
                  {
                      Answer.Add(i, seam);//字典添加字符串序号,出现次数
                  }
            }
            foreach (int i in Answer.Keys)
            {
                Console.WriteLine("字符串:" + array[i] + "出现" + (Answer[i]+1).ToString()+ "次");
            }
            Console.ReadLine();
        }

方法一:

 public void fun()
        {
            Dictionary<SortedDictionary<char, int>, int> conpare = new Dictionary<SortedDictionary<char, int>, int>();
            List<SortedDictionary<char, int>> list = new List<SortedDictionary<char, int>>();
            string input = "ABCCD,CDA,BCDD,DCA,ADC,BCD,CDCAB";
            string[] array = input.Split(',');
            List<int> Over = new List<int>();
            Dictionary<int, int> Answer = new Dictionary<int, int>(); 
            for (int i = 0; i < array.Length; i++)
            {
                char[] cc = array[i].ToCharArray();
                SortedDictionary<char, int> strcount = new SortedDictionary<char, int>();
                foreach (char a in cc)
                {
                    if (strcount.ContainsKey(a))
                        strcount[a] = strcount[a] + 1;
                    else
                        strcount.Add(a, 1);
                }
                list.Add(strcount);
            }
            for (int i = 0; i < list.Count; i++)
            {

if (Over.Contains(i))//已经被后面处理过了就直接跳过。
                        continue;
                int seam = 0;
                SortedDictionary<char, int> strfirst = list[i];
                for (int j = i + 1; j < list.Count; j++)
                {
                    if (Over.Contains(j))//已经被后面处理过了就直接跳过。
                        continue;
                    bool flag = true;
                    SortedDictionary<char, int> strsecond = list[j];
                    if (strsecond.Count != strfirst.Count)//字符串长度不一致
                    {
                        flag = false;
                        continue;
                    }
                    foreach (char a in strfirst.Keys)
                    {
                        if (!strsecond.ContainsKey(a))//字母不一样
                        {
                            flag = false;
                            break;
                        }
                        if (strsecond[a] != strfirst[a])//字母出现次数不一样
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                    {
                        Over.Add(j);//已经处理过的字符串
                        // list.Remove(list[j]);                       
                        seam++;
                    }
                }
                if (seam > 0)
                {
                    Answer.Add(i, seam);
                }
            }     
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值