要求:

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

输出:

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

一些基本的测试用例:

输出字符串中最长的单词 C# 算法的更多相关文章

  1. 基于python 3.5 所做的找出来一个字符串中最长不重复子串算法

    功能:找出来一个字符串中最长不重复子串 def find_longest_no_repeat_substr(one_str): #定义一个列表用于存储非重复字符子串 res_list=[] #获得字符 ...

  2. Java实现 LeetCode 720 词典中最长的单词(字典树)

    720. 词典中最长的单词 给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最 ...

  3. 7-19 计算有n个字符串中最长的字符串长度 (40 分)

    编写程序,用于计算有n(1<n<10)个字符串中最长的字符串的长度.前导空格不要计算在内! 输入格式: 在第一行中输入n,接下的每行输入一个字符串 输出格式: 在一行中输出最长的字符串的长 ...

  4. 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4)将字符串“hello”替换为“me”,输出新字符串。 5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 */

    namespace test4 {/* 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度. 2)输出字符串中第一个出现字母a的位置. 3)在字符串的第3个字符 ...

  5. js:重复输出字符串中字符

    复习了 重复输出一个字符串后, 重复输出一个字符串是 比如给定 str:abc  num:3 要求输出 abcabcabc 文章链接:https://www.cnblogs.com/mobu/p/98 ...

  6. 字符串中连续出现最多的子串 &amp; 字符串中最长反复子串

    字符串中连续出现最多的子串 & 字符串中最长反复子串 字符串中连续出现最多的子串 & 字符串中最长反复子串,这两个问题都能够用后缀数组来表示,至于后缀数组能够參考编程珠玑P156:后缀 ...

  7. JS返回一个字符串中长度最小的单词的长度

    题目:编写一个方法,返回字符串中最小长度的单词的长度. var str = 'What a good day today!'; 1 //方法一 2 function returnString1(str ...

  8. python-又来练习题--输出一个字符串中最长的子字符串及其长度

    一.有个字符串 str= '$sd1#111$svda123!!!221&eSSDSyyyyyyDG^svda121^svda124^1111111111111' 包含特殊字符.数字和字母,输 ...

  9. Python习题-输出一个字符串中最长的子字符串及其长度

    描述:有个字符串$sd1#111$svda123!!!221&eSSDSDG,包含特殊字符.数字和字母,输出最长的子字符串和他的长度#例如上面的字符串包含数字字母的字符串是svda123,长度 ...

随机推荐

  1. alembic教程

    安装 pip install alembic 步骤 1.初始化 alembic 仓库 在终端中, cd 到你的项目目录中,然后执行命令 alembic init alembic ,创建一个名叫 ale ...

  2. Oracle EXPDP/IMPDP示例

    待整理: 参考DAVE博客 http://blog.csdn.net/tianlesoftware/article/details/6260138

  3. 前端基础——css

    前端基础——css css的内容主要包括:盒子模型.定位.单位与取值.属性.选择器.

  4. android stdio 编译项目报Error:Failed to find target with hash string 'android-24

    android stdio 编译项目报Error:Failed to find target with hash string 'android-24 查看已有的SDK 设置项目的sdk为 25 an ...

  5. 剑指offer三从头到尾打印链表

    一.题目: 输入一个链表,从尾到头打印链表每个节点的值. 二.解题方法: 方法一:采用递归的方式实现 方法二:借助堆栈的“后进先出”实现 import java.util.ArrayList; imp ...

  6. 基于sql service会话共享,实现SSO

    1:session的存储基于sql service数据库来存储 2:修改sql service中会话管理的系统存储过程 3:实现几个站点的会话共享 4:应用共享会话,实现单点登录

  7. Referrer Policy 介绍

    发布于 署名 4.0 国际 (CC BY 4.0) 原文链接:https://caixw.io/posts/2017/referrer-policy.html 当用户在浏览器上点击一个链接时,会产生一 ...

  8. Android的ListView分页功能(上滑加载更多)

    今天主要工作是将之前实现的各种ListView显示全部信息,优化成了每次加载几条数据,然后上滑的时候加载更多,底部显示一个进度条和一个文字提示,然后加载完毕后,将提示信息隐藏. 一边看教学视频一遍敲代 ...

  9. JavaScript -- Location

    -----043-Location.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&quo ...

  10. Wookmark-jQuery-master 瀑布流插件使用介绍,含个人测试DEMO

    要求 必备知识 本文要求基本了解 Html/CSS,  JavaScript/JQuery. 开发环境 Dreamweaver CS6 / Chrome浏览器 演示地址 演示地址 资料下载   测试预 ...