Andy's First Dictionary

原题及翻译

Andy, 8, has a dream - he wants to produce his very own dictionary.
8岁的安迪有一个梦想——他想自己编一本字典。
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.
当然,这是一项非常耗时的工作,而这正是计算机程序有帮助的地方。
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”等词必须视为相同。

Input

The input file is a text with no more than 5000 lines.
输入文件是不超过5000行的文本。
An input line has at most 200 characters.
输入行最多有200个字符。
Input is terminated by EOF.
输入被EOF终止。

Output

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.
您可以确定,文本中不同单词的数目不超过5000。

Sample Input

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.

Sample Output

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

代码分析

#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;
set<string> dict;  //string集合
int main()
{
	string s,buf;
	while(cin>>s)
	{
		for(int i=0;i<s.length();i++)
		{
			if(isalpha(s[i]))
			{
				s[i]=tolower(s[i]);
			}
			else
			{
				s[i]=' ';
			}
		}
		stringstream ss(s);
		while(ss>>buf)
		dict.insert(buf);
	}
	for(set<string>::iterator it=dict.begin();it!=dict.end();++it)
	{
		cout<<*it<<"\n";
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AlexKing007/p/12339379.html