首页 > 代码库 > UVa-10815 安迪的第一个词典

UVa-10815 安迪的第一个词典

字符函数库中常用的函数

函数名称 返回值
isalnum() 如果参数是字母数字,即字母或数字,该函数返回true
isalpha() 如果参数是字母,该函数返回真
isblank() 如果参数是空格或水平制表符,该函数返回true
iscntrl() 如果参数是控制字符,该函数返回true
isdigit() 如果参数是数字(0~9),该函数返回true
isgraph() 如果参数是除空格之外的打印字符,该函数返回true
islower() 如果参数是小写字母,该函数返回true
isprint() 如果参数是打印字符(包括空格),该函数返回true
ispunct() 如果参数是标点符号,该函数返回true
isspace()

如果参数是标准空白字符,如空格、进纸、换行符、回车

、水平制表符或者垂直制表符,该函数返回true

isupper() 如果参数是大写字母,该函数返回true
isxdigit() 如果参数是十六进制的数字,即0~9、a~f、A~F,该函数返回true
tolower() 如果参数是大写字符,则返回其小写,否则返回该参数
toupper() 如果参数是小写字母,则返回其大写,否则返回该参数

题目

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

分析

这个题比较简单,我学习的是《算法竞赛入门经典》这本书的代码。

1.学会运用set容器;(有个小知识点,set容器,元素只能出现一次,并且插入可以从小到大排序)

2.学习字符函数库中常用的函数

3.学会stringstream(可参考这篇博文:http://blog.csdn.net/xw20084898/article/details/21939811)

4.最后运行记得是  在空行  ctrl+z +回车。(至于为什么,参考博文:http://blog.csdn.net/kevin_ut/article/details/8576740)

 1 #include<iostream>
 2 #include<string>
 3 #include<set>
 4 #include<sstream> 
 5 using namespace std;
 6 set<string>a;
 7 int main()
 8 {
 9     string s,buf;
10     while(cin>>s)
11     {
12         for(int i=0;i<s.length();i++)
13         {
14             if(isalpha(s[i]))s[i]=tolower(s[i]);else s[i]= ;
15         }
16             stringstream ss(s);
17             while(ss>>buf) a.insert(buf);
18         
19     }
20     for(set<string>::iterator it=a.begin();it!=a.end();++it)//迭代器 
21         cout<<*it<<"\n";
22     return 0;
23 }

 

UVa-10815 安迪的第一个词典