UVa10815

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 }

时间: 2024-10-27 01:30:03

UVa10815的相关文章

UVa10815 Andy&#39;s First Dictionary (STL)

链接:http://acm.hust.edu.cn/vjudge/problem/18649分析:set容器应用.set中每个元素最多只出现一次.自定义类型也可以构造set,必须定义“小于”运算符.set中的元素从小到大已排好序. 1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 #include <set> 5 #include <sstream> 6 using n

UVA10815 Andy&#39;s First Dictionary

问题链接:UVA10815 Andy's First Dictionary. 题意简述:输入一个文本文件,从中提取出字典,重复的单词被去掉. 这个问题用C++语言编写程序,主要是为了练习使用STL的功能.另外一点,C++编写程序效率会更高. 使用STL容器类的set,可以方便地去重复,而且还会自动排序. 程序中,使用C语言的库函数strtok()来切割单词,并且用空格' '作为分隔符.这是一种简便的做法. 另外一种切割字符串的方法是,使用STL的字符串流(sstream)实现. AC的C++程序

UVa10815,Andy&#39;s First Dictionary, set,stringstream

题意: 输入一个文本,找出所有不同的单词,按字典序输出所有单词. stringstream通常是用来做数据转换的 相比c库的转换,它更加安全,自动和直接 #include <sstream> stringstream stream stream <<value   输入 stream >>value   读取 #include <cstdio> #include <set> #include <string> #include <

安迪的第一个字典 (Andy&#39;s First Dictionary,UVa10815)

题目描述: #include<iostream> #include<string> #include<set> #include<sstream> using namespace std; set<string> dic; string s, buf; int main() { while(cin >> s) { for(int i = 0; i < s.length(); i++) if(isalpha(s[i])) s[i]

UVa-10815 安迪的第一个词典

字符函数库中常用的函数 函数名称 返回值 isalnum() 如果参数是字母数字,即字母或数字,该函数返回true isalpha() 如果参数是字母,该函数返回真 isblank() 如果参数是空格或水平制表符,该函数返回true iscntrl() 如果参数是控制字符,该函数返回true isdigit() 如果参数是数字(0-9),该函数返回true isgraph() 如果参数是除空格之外的打印字符,该函数返回true islower() 如果参数是小写字母,该函数返回true ispr

uva10815(set的应用)

紫书例题,这道题的例程让我长了知识.以前没有用过cctype和stringstream相关的东西.很实用,值得学习. #include <cctype>的函数 c++中应该是#include <cctype> c中应该是#include <ctype.h> 以下为字符函数库中常用的函数: 函数名称 返回值 isalnum() 如果参数是字母数字,即字母或数字,该函数返回true isalpha() 如果参数是字母,该函数返回真 isblank() 如果参数是空格或水平制

uva10815 by sixleaves

题目很简单.其实stringstream就的用法和iosteam差不多,所以学习起来是很简单的.stringstream类里面有一个string缓存,str()和str(string)成员函数.前者用于把stringstream的返回string缓存,后者是把string对象包装成stringstream对象.stringstream类存在于sstream头文件中.其是独立于标准I/O但有类似功能的类.clear主要是为了多次使用stringstream,因为stringstream每次的构造都

uva10815安迪的第一个字典

背景:这种题,我只能说,原谅我是新手,一点不会,于是又只有照着书上打出来. 学习:c++里面set的一些基本用法.还有isalpha()函数和tolower()函数,isalpha()函数相当于isupper()||islower()的作用,为判断一个字符是否为英文字符,tolower()为将一个字符转变成小写字符,与之相反的为tosupper()函数.代码中的set<string>::iterator中的iterator是迭代器的意思,是STL中的重要概炼,类似于指针. 注:set和stri

数据结构之set UVa10815

1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <sstream> 6 #include <vector> 7 #include <set> 8 9 using namespace std; 10 11 set<string> arr; 12 13 int main(