Andy’s First Dictionary 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. 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. Input
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.
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.
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
Universidad
de
in
left
read
road
sign
so
the
they
to
two
went
were
when
题意:
给你一个文本,你需要输出里面出现的所有的英文单词,不区分大小写,全部以小写的格式予以输出(按照字典序)。
输入:
字符文本。
输出:
每行一个单词。按字典序输出。
分析:
注意到单词与单词中间可以用空格、标点字符、数字等隔开,并且单词的字母不区分大小写。于是我们采用string类来进行输入,对输入的每一个字符都判断它是不是字母,若是字母就全部转化成小写的。存储的话,开一个set数组,自然而然地解决了字典序的问题。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <vector> 5 #include <string> 6 #include <algorithm> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 int main(){ 11 set<string> dict; 12 string s; 13 while(cin >> s){ 14 string tmp = ""; 15 bool key = false; 16 for(int i = 0 ; i < s.length() ; i++){ 17 if(isalpha(s[i])){ 18 if(s[i]>= ‘A‘ && s[i] <= ‘Z‘) 19 s[i] = s[i] - ‘A‘ + ‘a‘; 20 tmp += s[i]; 21 key = true; 22 } 23 else{ 24 if(key){ 25 dict.insert(tmp); 26 tmp = ""; 27 } 28 key = false; 29 } 30 } 31 if(key) dict.insert(tmp); 32 } 33 set<string>::iterator it; 34 for(it = dict.begin() ; it != dict.end() ; it++) 35 cout << *it << endl; 36 return 0; 37 }