zoukankan      html  css  js  c++  java
  • 输出字符串中最长的单词 C# 算法

    要求:

    1. 设计一个算法从一片英语文章或者英语字符串里面输出其中最长的单词.

               Input: string     Output: string

    1. 尽可能多的设计测试用例来测试这个算法.
    2. 考虑空间和时间复杂度看作是一个加分项

    Version1.0

     1 using System;
     2 
     3 namespace GetLongestWord
     4 {
     5     class Program
     6     {
     7         static void Main(string[] args)
     8         {
     9             Console.WriteLine("Please input a string:");
    10             //string inputString = "Hello Shanghai Nice To meet you guys";
    11             string inputString = Console.ReadLine();
    12             Program p = new Program();
    13             Console.WriteLine("The longest word is : {0}", p.GetLongestWord(inputString));
    14             Console.WriteLine("The length of the longest word is: {0}", p.GetLongestWord(inputString).Length);
    15             Console.ReadKey();
    16         }
    17 
    18         string GetLongestWord(string inputString)
    19         {
    20             int maxLength = 0;
    21             int wordLength = 0;
    22             int p = 0;
    23             for (int i = 0; i < inputString.Length; i++)
    24             {
    25                 if (inputString[i] != ' ')
    26                 {
    27                     wordLength++;
    28 
    29                     if (maxLength < wordLength)
    30                     {
    31                         maxLength = wordLength;
    32                         p = i + 1 - wordLength;
    33                     }
    34                 }
    35                 else
    36                 {
    37                     wordLength = 0;
    38                 }
    39             }
    40 
    41             string longestWord = "";
    42             for (int i = 0, m = p; i < maxLength; i++, m++)
    43             {
    44                 longestWord = longestWord + inputString[m];
    45             }
    46 
    47             return longestWord;
    48         }
    49     }
    50 }
    View Code

    Version1.1

     1  string GetLongestWord(string inputString)
     2         {
     3             // Separate the string by blank spaces and store it to an array.
     4             string longestWord = "";
     5             string[] stringArray = inputString.Split(' ');
     6 
     7             // Assign the longest word to longestword.
     8             for (int i = 0; i < stringArray.Length; i++)
     9             {
    10                 if (longestWord.Length < stringArray[i].Length)
    11                 {
    12                     longestWord = stringArray[i];
    13                 }
    14 
    15             }
    16 
    17             return longestWord;
    18         }
    View Code

    分析优缺点:Version1.0 and Version1.1 虽然实现了基本功能,但是只假设string分隔符默认是空格,也没有考虑字符串相等的word有多个的情况。

    Version1.2

     1 using System;
     2 using System.Collections;
     3 
     4 namespace GetLongestWord
     5 {
     6     class Version2
     7     {
     8         static void Main()
     9         {
    10             Console.WriteLine("Please input a string:");
    11             //string inputString = "Hello Shanghai Nice To meet you guys";
    12             string inputString = Console.ReadLine();
    13             Version2 p2 = new Version2();
    14             Console.WriteLine("The longest words are: ");
    15             ArrayList al = p2.GetLongestWord(inputString);
    16             for (int m = 0; m < al.Count; m++)
    17             {
    18                 Console.WriteLine(al[m]);
    19             }
    20             string longestWord = (string)al[0];
    21             Console.WriteLine("The lenght of the longest word(s) is: {0}", longestWord.Length);
    22             Console.WriteLine("There are {0} longest word(s)", p2.GetLongestWord(inputString).Count);
    23             Console.ReadKey();
    24         }
    25 
    26         // Deal with the situation when there are more than one longest words. 
    27         // Use ArrayList.
    28         ArrayList GetLongestWord(string inputString)
    29         {
    30             string longestWord = "";
    31             ArrayList al = new ArrayList();
    32             string[] stringArray = inputString.Split(' ');
    33             for (int i = 0; i < stringArray.Length; i++)
    34             {
    35                 if (longestWord.Length < stringArray[i].Length)
    36                 {
    37                     longestWord = stringArray[i];
    38                     al.Add(longestWord);
    39                 }
    40                 else if (longestWord.Length == stringArray[i].Length)
    41                 {
    42                     al.Add(stringArray[i]);
    43                 }
    44             }
    45 
    46             return al;
    47         }
    48     }
    49 }
    View Code

    大家有没有发现1.2版本的bug呢?

    修改后

    Version1.3

     1 ArrayList GetLongestWord(string inputString)
     2         {
     3             string longestWord = "";
     4             ArrayList al = new ArrayList();
     5             string[] stringArray = inputString.Split(' ');
     6             for (int i = 0; i < stringArray.Length; i++)
     7             {
     8                 if (longestWord.Length <= stringArray[i].Length)
     9                 {
    10                     longestWord = stringArray[i];
    11                 }
    12             }
    13             al.Add(longestWord);
    14             return al;
    15         }
    View Code

    Version1.4

     1 using System;
     2 using System.Collections;
     3 using System.IO;
     4 
     5 namespace GetLongestWord
     6 {
     7     class Version2
     8     {
     9         static void Main()
    10         {
    11             Console.WriteLine("Please input a string:");
    12             //string inputString = "Hello Shanghai Nice To meet you guys";
    13             //string inputString = Console.ReadLine();
    14             FileStream aFile = new FileStream(@"c:	est.txt", FileMode.Open);
    15             StreamReader sr = new StreamReader(aFile);
    16             string inputString = sr.ReadLine();
    17             sr.Close();
    18             Version2 p2 = new Version2();
    19             Console.WriteLine("The longest words are: ");
    20             ArrayList al = p2.GetLongestWord(inputString);
    21             for (int m = 0; m < al.Count; m++)
    22             {
    23                 Console.WriteLine(al[m]);
    24             }
    25             string longestWord = (string)al[0];
    26             Console.WriteLine("The lenght of the longest word(s) is: {0}", longestWord.Length);
    27             Console.WriteLine("There are {0} longest word(s)", al.Count);
    28             Console.ReadKey();
    29         }
    30 
    31         // Deal with the situation when there are more than one longest words. 
    32         // Use ArrayList.
    33         ArrayList GetLongestWord(string inputString)
    34         {
    35             ArrayList longestWords = new ArrayList();
    36             ArrayList al = new ArrayList();
    37             string[] stringArray = inputString.Split(' ');
    38             sortArrayByLength(stringArray);
    39             int maxLength=stringArray[0].Length;
    40             for (int i=0;i<stringArray.Length;i++)
    41             {
    42                 if (stringArray[i].Length==maxLength)
    43                 {
    44                     al.Add(stringArray[i]);
    45                 }
    46             }
    47         
    48             return al;
    49         }
    50 
    51         string[] sortArrayByLength(string [] inputArray)
    52         {
    53             for (int i=0;i<inputArray.Length-1;i++)
    54             {
    55                 for (int j=inputArray.Length-1;j> i;j--)
    56                 {
    57                     if (inputArray[i].Length<inputArray[j].Length)
    58                     {
    59                         string tmp = inputArray[j];
    60                         inputArray[j] = inputArray[i];
    61                         inputArray[i] = tmp;
    62                     }
    63                 }
    64                 
    65             }
    66             return inputArray;
    67         }
    68     }
    69 }
    View Code

    分析优缺点: Version1.4 考虑了字符串相等返回多个word的情况。可以从文件中读取string。用到了ArrayList。ArrayList比数组灵活,不需要提前分配指定长度的空间,非常适合这种长度不定的情况。但是没有考虑分隔符不是空格的情况,比如标点符号?

    继续优化: 我们将用正则表达式 把所有不是字母-的特殊字符都替换成空格,将GetLongestWord(string inputString)方法替换如下,其他地方不变。

    Version1.5

            // Deal with the situation when there are more than one longest words. 
            // Use ArrayList.
            // Replace all the non-letter characters with blank spaces.
            ArrayList GetLongestWord(string inputString)
            {
                ArrayList longestWords = new ArrayList();
                ArrayList al = new ArrayList();
                // Replace all the non-word characters with blank spaces.
                Regex reg = new Regex("[^a-zA-Z-]+");
                string inputArgs = reg.Replace(inputString," ");
                string[] stringArray = inputArgs.Split(' ');
                sortArrayByLength(stringArray);
                int maxLength = stringArray[0].Length;
                for (int i = 0; i < stringArray.Length; i++)
                {
                    if (stringArray[i].Length == maxLength)
                    {
                        al.Add(stringArray[i]);
                    }
                }
    
                return al;
            }
    

     输入: Hello OSTC World Shanghai!!!!!!!! Hi How are you Shanghai-China??????????

    输出:

     

     缺点: 好像不支持换行,时间空间复杂度不够好。

    一些基本的测试用例:

  • 相关阅读:
    rmq +二分暴力 hdu 5726
    8.25 ccpc 比赛总结
    莫比乌斯反演题目总结
    HDU 4848 Wow! Such Conquering! (搜索+floyd)
    Codeforces 982 C Cut 'em all!(DFS)
    Codefoces 986C AND Graph(DFS)
    CodeForces 986A Fair(BFS)
    ACM经验贴
    Kattis A+B Problem(FFT)
    CF E. Porcelain (双向dp)
  • 原文地址:https://www.cnblogs.com/I-am-Betty/p/5129258.html
Copyright © 2011-2022 走看看