zoukankan      html  css  js  c++  java
  • 给定一个单词,从字典查找该单词的所有兄弟单词。

         最近在网上看到百度的一个面试题:一个单词单词字母交换,可得另一个单词,如army->mary,成为兄弟单词。提供一个单词,在字典中找到它的兄弟。描述数据结构和查询过程。

         我的思路是这样的,所谓A单词是B单词的兄弟单词,无非就是组成A和B两个单词的所有字母都是一样,无非就是顺序不一样罢了。因此只要把单词按照AscII值来进行排序,然后判断下两个拍好序的单词是否完全一样,如果完全一样就是兄弟单词,否则就不是。下面是我用C#实现的代码。供参考,欢迎大家提供更好的思路。

          

           static void Main(string[] args)

            { 

                string[] Vocabularys = { "abcd", "ab", "army", "mary", "mnay", "mray", "myar", "rmay", "abcd" };

                Vocabularys = GetBrotherVocabularys(Vocabularys, "army");

                Console.WriteLine("the vocabularies of army's brother vocabulary are :");

                foreach (string vocabulary in Vocabularys)

                {

                    Console.Write("{0} ", vocabulary);

                }

                Console.Read();

            }

            /// <summary>

            /// 把一个字符串单词转化为一个字符列表

            /// </summary>

            /// <param name="vocabulary"></param>

            /// <returns></returns>

            private static IList<char> GetCharListByVoca(string vocabulary)

            {

                return (from m in vocabulary.ToCharArray() orderby m ascending select m).ToList();

            }

     

            /// <summary>

            /// 给定一个字符串数组,返回该数组中的是给定字符串的兄弟字符串的那些字符串

            /// </summary>

            /// <param name="vocabularys">字符串数组</param>

            /// <param name="keyVocabulary">给定字符串</param>

            /// <returns></returns>

            private static string[] GetBrotherVocabularys(string[] vocabularys, string keyVocabulary)

            {

                List<string> lst = new List<string>();

                IList<char> keyList = GetCharListByVoca(keyVocabulary);

                int len = keyVocabulary.Length;

                foreach (string item in vocabularys)

                {

                    if (item.Length == len)

                    {

                        if (IsBrotherVoca(item, keyList))

                        {

                            lst.Add(item);

                        }

                    }

                }

                return lst.ToArray();

            }

     

            private static bool IsBrotherVoca(string vocabulary, IList<char> keyVocaCharList)

            {

                IList<char> vocaCharList = GetCharListByVoca(vocabulary);        

                int i = 0;

                bool flag = true;

                foreach (char c in vocaCharList)

                {

                    if (c != keyVocaCharList[i])

                    {

                        flag = false;

                        break;

                    }

                    i++;

                }

                return flag;

            }

     

     

      运行结果如下:

  • 相关阅读:
    钱多多软件制作04
    团队项目01应用场景
    HDU 4411 arrest
    HDU 4406 GPA
    HDU 3315 My Brute
    HDU 3667 Transportation
    HDU 2676 Matrix
    欧拉回路三水题 POJ 1041 POJ 2230 POJ 1386
    SPOJ 371 BOXES
    POJ 3422 Kaka's Matrix Travels
  • 原文地址:https://www.cnblogs.com/shaosks/p/2487602.html
Copyright © 2011-2022 走看看