输出字符串中最长的单词 C# 算法

时间:2024-04-30 09:50:00

要求:

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

Input: string     Output: string

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

Version1.0

 using System;

 namespace GetLongestWord
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input a string:");
//string inputString = "Hello Shanghai Nice To meet you guys";
string inputString = Console.ReadLine();
Program p = new Program();
Console.WriteLine("The longest word is : {0}", p.GetLongestWord(inputString));
Console.WriteLine("The length of the longest word is: {0}", p.GetLongestWord(inputString).Length);
Console.ReadKey();
} string GetLongestWord(string inputString)
{
int maxLength = ;
int wordLength = ;
int p = ;
for (int i = ; i < inputString.Length; i++)
{
if (inputString[i] != ' ')
{
wordLength++; if (maxLength < wordLength)
{
maxLength = wordLength;
p = i + - wordLength;
}
}
else
{
wordLength = ;
}
} string longestWord = "";
for (int i = , m = p; i < maxLength; i++, m++)
{
longestWord = longestWord + inputString[m];
} return longestWord;
}
}
}

Version1.1

  string GetLongestWord(string inputString)
{
// Separate the string by blank spaces and store it to an array.
string longestWord = "";
string[] stringArray = inputString.Split(' '); // Assign the longest word to longestword.
for (int i = ; i < stringArray.Length; i++)
{
if (longestWord.Length < stringArray[i].Length)
{
longestWord = stringArray[i];
} } return longestWord;
}

输出字符串中最长的单词 C# 算法

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

Version1.2

 using System;
using System.Collections; namespace GetLongestWord
{
class Version2
{
static void Main()
{
Console.WriteLine("Please input a string:");
//string inputString = "Hello Shanghai Nice To meet you guys";
string inputString = Console.ReadLine();
Version2 p2 = new Version2();
Console.WriteLine("The longest words are: ");
ArrayList al = p2.GetLongestWord(inputString);
for (int m = ; m < al.Count; m++)
{
Console.WriteLine(al[m]);
}
string longestWord = (string)al[];
Console.WriteLine("The lenght of the longest word(s) is: {0}", longestWord.Length);
Console.WriteLine("There are {0} longest word(s)", p2.GetLongestWord(inputString).Count);
Console.ReadKey();
} // Deal with the situation when there are more than one longest words.
// Use ArrayList.
ArrayList GetLongestWord(string inputString)
{
string longestWord = "";
ArrayList al = new ArrayList();
string[] stringArray = inputString.Split(' ');
for (int i = ; i < stringArray.Length; i++)
{
if (longestWord.Length < stringArray[i].Length)
{
longestWord = stringArray[i];
al.Add(longestWord);
}
else if (longestWord.Length == stringArray[i].Length)
{
al.Add(stringArray[i]);
}
} return al;
}
}
}

输出字符串中最长的单词 C# 算法

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

输出字符串中最长的单词 C# 算法

修改后

Version1.3

 ArrayList GetLongestWord(string inputString)
{
string longestWord = "";
ArrayList al = new ArrayList();
string[] stringArray = inputString.Split(' ');
for (int i = ; i < stringArray.Length; i++)
{
if (longestWord.Length <= stringArray[i].Length)
{
longestWord = stringArray[i];
}
}
al.Add(longestWord);
return al;
}

Version1.4

 using System;
using System.Collections;
using System.IO; namespace GetLongestWord
{
class Version2
{
static void Main()
{
Console.WriteLine("Please input a string:");
//string inputString = "Hello Shanghai Nice To meet you guys";
//string inputString = Console.ReadLine();
FileStream aFile = new FileStream(@"c:\test.txt", FileMode.Open);
StreamReader sr = new StreamReader(aFile);
string inputString = sr.ReadLine();
sr.Close();
Version2 p2 = new Version2();
Console.WriteLine("The longest words are: ");
ArrayList al = p2.GetLongestWord(inputString);
for (int m = ; m < al.Count; m++)
{
Console.WriteLine(al[m]);
}
string longestWord = (string)al[];
Console.WriteLine("The lenght of the longest word(s) is: {0}", longestWord.Length);
Console.WriteLine("There are {0} longest word(s)", al.Count);
Console.ReadKey();
} // Deal with the situation when there are more than one longest words.
// Use ArrayList.
ArrayList GetLongestWord(string inputString)
{
ArrayList longestWords = new ArrayList();
ArrayList al = new ArrayList();
string[] stringArray = inputString.Split(' ');
sortArrayByLength(stringArray);
int maxLength=stringArray[].Length;
for (int i=;i<stringArray.Length;i++)
{
if (stringArray[i].Length==maxLength)
{
al.Add(stringArray[i]);
}
} return al;
} string[] sortArrayByLength(string [] inputArray)
{
for (int i=;i<inputArray.Length-;i++)
{
for (int j=inputArray.Length-;j> i;j--)
{
if (inputArray[i].Length<inputArray[j].Length)
{
string tmp = inputArray[j];
inputArray[j] = inputArray[i];
inputArray[i] = tmp;
}
} }
return inputArray;
}
}
}

输出字符串中最长的单词 C# 算法

分析优缺点: 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??????????

输出:

输出字符串中最长的单词 C# 算法

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

一些基本的测试用例:

输出字符串中最长的单词 C# 算法