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<bits/stdc++.h> using namespace std; string s; set<string>se; int main() { while(getline(cin,s)) { int len=s.size(); for(int i=0;i<s.size();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<<k<<endl; }//输出 }
9.反思
一定要注意标点符号对于单词的影响,要先处理掉标点再使用 stringstream 操作