输出字符串中最长的单词 C# 算法
要求:
- 设计一个算法从一片英语文章或者英语字符串里面输出其中最长的单词.
Input: string Output: string
- 尽可能多的设计测试用例来测试这个算法.
- 考虑空间和时间复杂度看作是一个加分项
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??????????
输出:

缺点: 好像不支持换行,时间空间复杂度不够好。
一些基本的测试用例:

输出字符串中最长的单词 C# 算法的更多相关文章
- 基于python 3.5 所做的找出来一个字符串中最长不重复子串算法
功能:找出来一个字符串中最长不重复子串 def find_longest_no_repeat_substr(one_str): #定义一个列表用于存储非重复字符子串 res_list=[] #获得字符 ...
- Java实现 LeetCode 720 词典中最长的单词(字典树)
720. 词典中最长的单词 给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最 ...
- 7-19 计算有n个字符串中最长的字符串长度 (40 分)
编写程序,用于计算有n(1<n<10)个字符串中最长的字符串的长度.前导空格不要计算在内! 输入格式: 在第一行中输入n,接下的每行输入一个字符串 输出格式: 在一行中输出最长的字符串的长 ...
- 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4)将字符串“hello”替换为“me”,输出新字符串。 5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 */
namespace test4 {/* 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度. 2)输出字符串中第一个出现字母a的位置. 3)在字符串的第3个字符 ...
- js:重复输出字符串中字符
复习了 重复输出一个字符串后, 重复输出一个字符串是 比如给定 str:abc num:3 要求输出 abcabcabc 文章链接:https://www.cnblogs.com/mobu/p/98 ...
- 字符串中连续出现最多的子串 & 字符串中最长反复子串
字符串中连续出现最多的子串 & 字符串中最长反复子串 字符串中连续出现最多的子串 & 字符串中最长反复子串,这两个问题都能够用后缀数组来表示,至于后缀数组能够參考编程珠玑P156:后缀 ...
- JS返回一个字符串中长度最小的单词的长度
题目:编写一个方法,返回字符串中最小长度的单词的长度. var str = 'What a good day today!'; 1 //方法一 2 function returnString1(str ...
- python-又来练习题--输出一个字符串中最长的子字符串及其长度
一.有个字符串 str= '$sd1#111$svda123!!!221&eSSDSyyyyyyDG^svda121^svda124^1111111111111' 包含特殊字符.数字和字母,输 ...
- Python习题-输出一个字符串中最长的子字符串及其长度
描述:有个字符串$sd1#111$svda123!!!221&eSSDSDG,包含特殊字符.数字和字母,输出最长的子字符串和他的长度#例如上面的字符串包含数字字母的字符串是svda123,长度 ...
随机推荐
- pageadmin CMS自助建站系统教程:模板中执行sql语句
PageAdmin系统提供了一个内置的数据库访问对象,声明如下: DataBaseContext dbContext = DbHelper.DbContext(); 通过DataBaseContext ...
- 讲讲我当年是怎么拿到AI研发公司offer的
前言 很多的老铁私信问我,当年我是怎么拿到公司offer的,我记得我毕业是2015年,那时人工智能这个行业还没热起来,能提供的岗位很少但是面试的人更少,我又是本专业毕业的,所以当初找工作还算顺利,去面 ...
- Weblogic 错误 <BEA-000403> <BEA-000438>解决办法
控制台提示如下错误: <Error> <Socket> <BEA-000438> <Unable to load performance pack. Us ...
- JavaScript(JS)简介
历史背景介绍 (Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript ...
- Unity项目接入应用宝SDK实现截图功能
Unity项目接入应用宝SDK实现截图功能 问题由来 点击应用宝悬浮窗 如图所示 左下角有一个截图按钮 需要解决那些问题 截图信息需要由游戏引擎提供 SDK获取截图信息为同步 但是Unity引擎没有提 ...
- [原创]K8 Struts2 Exp 20170310 S2-045(Struts2综合漏洞利用工具)
工具: K8 Struts2 Exploit组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.blog.163.com发布: 2014/7/31 10:24 ...
- (转)Python3之shutil模块
原文:https://www.cnblogs.com/wang-yc/p/5625046.html 一. 简介 shutil 是高级的文件,文件夹,压缩包处理模块. 二. 使用 shutil.copy ...
- 说说正则表达式的exec方法
话说,关于正则表达式有一个梗,大意是: 假如你有一个问题,想用正则来解决,于是你就有了两个问题 这句话侧面反映了精通正则是一件不容易的事.比如我今天遇到的诡异事件. 情景回放 这两天练手写了一个爬用户 ...
- Java内存模型(JSR133)问与答
What is a memory model, anyway? In multiprocessor systems, processors generally have one or more lay ...
- solr(一) : 整合 tomcat
前面 lucene 初探 都是为了solr打基础的. 虽然lucene 的filter 没有涉及, 但是打基础, 差不多够用了. 一. solr 和 lucene 的区别 这里我就用自己的理解来说了, ...