栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Andy’s First Dictionary( stringstream )

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Andy’s First Dictionary( stringstream )

文章目录
    • 1.描述:
    • 2. 输入:
    • 3.输出:
    • 4.样例输入:
    • 5.样例输出:
    • 6.题目大意:
    • 7.思路
    • 8.代码
    • 9.反思

1.描述:

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.
8岁的安迪有一个梦想——他想制作自己的字典。这对他来说不是一项容易的任务,因为他知道的单词数量还不够。他没有自己想出所有的单词,而是有了一个聪明的想法。他会从书架上挑选一本他最喜欢的故事书,从中抄写所有不同的单词。按字母顺序排列单词,他就完成了!当然,这是一项非常耗时的工作,而这正是计算机程序有用的地方。

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like “Apple”, “apple” or “APPLE” must be considered the same.
你被要求编写一个程序,列出输入文本中的所有不同单词。
在这个问题中,单词被定义为字母的连续序列,大写和/或小写。只有一个字母的单词也要考虑。此外,您的程序必须不区分大小写。例如,像“Apple”、“apple”或“APPLE”这样的词必须被认为是相同的。

2. 输入:

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

3.输出:

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

4.样例输入:
Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.
5.样例输出:
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
6.题目大意:

给出几行句子,找出所有不同的单词,按字典序排序输出

7.思路

用set储存单词,自动去重和排序,用stringstream处理句子,先去掉各种符号并处理成小写,再存入set

8.代码
#include
using namespace std;

string s;
setse;

int main()
{
	while(getline(cin,s))
	{
		int len=s.size();
		
		for(int i=0;i
			if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
			{
				s[i]=tolower(s[i]);
			}//是字母小写
			else
			{
				s[i]=' ';
			}//是标点转换成空格
		}
		
		stringstream ss(s);
		while(ss>>s)
		{
			se.insert(s);
		}//存入set
	}
	
	for(auto k : se)
	{
		cout< 
9.反思 

一定要注意标点符号对于单词的影响,要先处理掉标点再使用 stringstream 操作

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847923.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号