輸出字符串中最長的單詞 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.1ide

 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呢?spa

修改後.net

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.43d

 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:\test.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??????????

輸出:

 

 缺點: 好像不支持換行,時間空間複雜度不夠好。

一些基本的測試用例:

相關文章
相關標籤/搜索