输出字符串中最长的单词 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,长度 ...
随机推荐
- Python 绝技 —— TCP服务器与客户端
i春秋作家:wasrehpic 0×00 前言 「网络」一直以来都是黑客最热衷的竞技场.数据在网络中肆意传播:主机扫描.代码注入.网络嗅探.数据篡改重放.拒绝服务攻击……黑客的功底越深厚,能做的就越多 ...
- mysql数据库崩溃:InnoDB: Database page corruption on disk or a failed
修改mysql配置文件my.cnf,添加 innodb_force_recovery = 6 innodb_purge_thread = 0 重启mysql 这时只可以执行select,create, ...
- python socket 编程简单入门
想讲讲套接字的概念 套接字,即英文socket的中文意译,起源于20世纪70年代,是加利福利亚大学的伯克利版本UNIX(称为BSD UNIX)的一部分.目的是实现主机上运行的一个程序与另一个运行的程序 ...
- python iter函数用法
iter函数用法简述 Python 3中关于iter(object[, sentinel)]方法有两个参数. 使用iter(object)这种形式比较常见. iter(object, sentinel ...
- Zookeeper--0300--java操作Zookeeper,临时节点实现分布式锁原理
删除Zookeeper的java客户端有 : 1,Zookeeper官方提供的原生API, 2,zkClient,在原生api上进行扩展的开源java客户端 3, 一.Zookeeper原生API ...
- Shell常见的快捷键
Ctrl + a - Jump to the start of the lineCtrl + b - Move back a charCtrl + c - Terminate the command ...
- 一段奇妙的vim编辑器之旅
一.背景 对于Linux服务器上的操作,我们往往少不了使用vim,而有时候我对vim的使用并没有那么的熟练和深入,这周就深入的学习了vim的使用,包括入门和进阶,先分享给你们,也方便自己以后复习查询. ...
- 《Java多线程编程核心技术》——多线程与同步
Java多线程 线程可以理解为是在进程中独立运行的子任务. Java多线程 使用方法 Java中实现多线程主要有以下两种方法: 继承Thread,而后实例化该对象调用start()即启动了新线程; 实 ...
- 使用node和express+mongodb实现数据增删改功能
2018即将过去,2019即将来临,前端技术不断在在更新,学的东西越来越多.我们只有不断的学习,才不能被淘汰.在前后端分离的一个时代,后端提供接口,前端调用接口,逻辑判断,每个都是独立的工作.如果自己 ...
- docker内存和cpu调试
本地启动了一个sshd的容器服务,但该容器经常会被重启导致ssh连接失败,使用kubectl describe pod命令查看改命令发现有容器返回值为137,一般是系统环境原因,且一般为内存不足导致的 ...