要求:

  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;
}

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

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

修改后

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;
}
}
}

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

输出:

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

一些基本的测试用例:

最新文章

  1. T-SQL:毕业生出门需知系列(七)
  2. 利用CSS3中transparent实现三角形及三角形组合图
  3. 实验一 cmd命令的编写
  4. ffmpeg-20160701-git-bin.7z
  5. 通过AngularJS实现图片上传及缩略图展示
  6. [学习笔记]RAID及实验
  7. KMP算法原理
  8. Promises与Javascript异步编程
  9. Chrome 开发者工具详解(2):Network 面板
  10. JS 网页打印解决方案
  11. 为什么不要在viewDidLoad方法中设置开始监听键盘通知
  12. Mark一下~
  13. Nginx grpc反向代理
  14. Kruskal算法(题目还是:畅通工程)
  15. hdu1358 Period kmp求循环节
  16. ProtoBuf 常用序列化/反序列化API 转
  17. mysql问题处理记录
  18. 2.1 Linux中wait、system 分析
  19. 【python】logging日志模块写入中文编码错误解决办法
  20. JSON对应的maven依赖包

热门文章

  1. 微信小程序如何跳转到另一个小程序
  2. MariaDB 视图与触发器(11)
  3. 13_python_内置函数
  4. Git 本地操作
  5. ssh免密码登录Permission denied (publickey,gssapi-keyex,gssapi-with-mic) 的解决方案!
  6. 微信小程序报Cannot read property &#39;setData&#39; of undefined的错误
  7. ElasticSearch入门3: Spring Boot集成ElasticSearch
  8. 杂谈:HTTP1.1 与 HTTP2.0 知多少?
  9. Vue笔记:使用 VS Code 断点调试
  10. java实现微信支付