10815 - Andy's First Dictionary
Time limit: 3.000 seconds
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 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
这题本人用c写的,很繁琐,看博客有一位高手总结的很好,转到这里存一下,博客地址:
http://www.cnblogs.com/zywscq/p/3979958.html
附代码:
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<cmath>
6 #include<map>
7 #include<set>
8 #include<vector>
9 #include<algorithm>
10 #include<stack>
11 #include<queue>
12 #include<cctype>
13 #include<sstream>
14 using namespace std;
15 #define INF 1000000000
16 #define eps 1e-8
17 #define pii pair<int,int>
18 #define LL long long int
19 #define maxn 100009
20 string s,buf;
21 set<string>dict;
22 int main()
23 {
24 //freopen("in.txt","r",stdin);
25 //freopen("out.txt","w",stdout);
26 while(cin>>s)
27 {
28 int len=s.length();
29 stringstream ss;
30 for(int i=0;i<len;i++)
31 {
32 if(isalpha(s[i])) s[i]=tolower(s[i]);
33 else s[i]=' ';
34 }
35 ss<<s;
36 /*也可以ss.str(s),如果清空ss的内容就用ss.str("")。注意ss.str(s)是覆盖掉ss中原来的东西,而ss<<s是在后面添加上s*/
37 while(ss>>buf)//空格都不会传
38 {
39 dict.insert(buf);
40 }
41 }
42 for(set<string>::iterator it=dict.begin();it!=dict.end();it++)
43 //注意不能写成it<dict.end(),只有等与不等
44 cout<<*it<<endl;
45 return 0;
46 }