UVa 10815 Andy's First Dictionary

来源:互联网 发布:变电仿真软件开发 编辑:程序博客网 时间:2024/06/18 00:21

Problem B: Andy's First Dictionary

Time limit: 3 seconds


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 DisneylandTwo blondes were going to Disneyland when they came to a fork in theroad. The sign read: "Disneyland Left."So they went home.

Sample Output

aadventuresblondescamedisneylandforkgoinghomeinleftreadroadsignsothetheytotwowentwerewhen


Joke and image taken from the Web

/*#include <cstdio>#include <set>#include <string>#include <cstring>#include <cstdlib>#include <iostream>using namespace std;char word[20000];char n_word[20000];int main(){set<string> dict;set<string>::iterator iter;memset(word, 0, sizeof(word));while(scanf("%s", word) != EOF){memset(n_word, 0, sizeof(n_word));int len = strlen(word);int count = 0;for(long long i = 0; i < len; i++){if(word[i] >= 'a' && word[i] <= 'z'){n_word[count] = word[i];count++;}else if(word[i] >= 'A' && word[i] <= 'Z'){n_word[count] = word[i] - 'A' + 'a';count++;}else{dict.insert(n_word);count = 0;memset(n_word, 0, sizeof(n_word));while(i+1 < len && !((word[i+1] >= 'a' && word[i+1] <= 'z') || (word[i+1] >= 'A' && word[i+1] <= 'Z')))i++;}}if(count != 0)dict.insert(n_word);memset(word, 0, sizeof(word));}for(iter = dict.begin(); iter != dict.end(); iter++)//printf("%s\n", *iter);cout<<*iter<<endl;return 0;}*/#include <iostream>#include <string>#include <sstream>#include <set>using namespace std;set<string> dict;int main(){string str, n_str;while(cin >> str){for(int i = 0; i < str.length(); i++){if(str[i] >= 'A' && str[i] <= 'Z')str[i] = str[i] - 'A' + 'a';else if(!(str[i] >= 'a' && str[i] <= 'z'))str[i] = ' ';}stringstream str_in(str);while(str_in >> n_str)dict.insert(n_str);}for(set<string>::iterator p = dict.begin(); p != dict.end(); p++)cout << *p << endl;return 0;}

题目本身不难,关键在于利用set性质:元素重复插入就会忽略,并且按字母表顺序排列输出。学会set上的insert和遍历操作,并用stringstream来读取字符串,把字符串当作输入流,方便。另外,isalpha(char x)来判断是否为字母,小写字母返回2,大写字母返回1,非字母返回0.tolower(char x)来将大写字母转换为小写字母,如果输入不是大写字母则原样返回
0 0
原创粉丝点击