zoukankan      html  css  js  c++  java
  • 安迪的第一个字典 Andy's First Dictionary, UVa 10815

    Description

    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. Further more, your program must be case insensltive.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. Inputis terminated by EOF.

    输入文件是不超过5000行的文本。输入行最多包含200个字符。EOF终止输入。

    Output

    Your output should give a list of different words that appears in the input text, one in a line. Thewords 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.

    输出请给出输入文本中出现的不同单词的列表,一个单词在一行中。这些字都应该是小写的,按字母顺序排列。文件中的不同单词的数量不超过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

    adventuresblondes

    came

    disneyland

    fork

    going

    home

    in

    left

    read

    road

    sign

    so

    the

    they

    to

    two

    went

    were

    when

    分析

    把读到的单词放入set中会自动排序,然后用迭代器按序输出即可

     1 #include <iostream>
     2 #include <string>
     3 #include <set>
     4 #include <sstream>
     5 
     6 using namespace std; 
     7 
     8 set<string> dict;    //string集合 
     9 
    10 int main()
    11 {
    12     string s, buf;
    13     while(cin >> s)
    14     {
    15         for(int i =  0; i < s.length(); i++)
    16         {
    17             if(isalpha(s[i]))    //判断字符是否为英文字母
    18                 s[i] = tolower(s[i]);    //将大写字母转换为小写字母 
    19             else
    20                 s[i] = ' ';
    21             stringstream ss(s);    //创建存储s的副本的stringstream对象
    22             while(ss >> buf)
    23             {
    24                 dict.insert(buf);
    25             }
    26         }
    27         for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
    28             cout << *it << "
    ";
    29         return 0;
    30     }
    31 }

    (程序只处理首字母大写的情况)

    set是STL中一种标准关联容器(矢量、列表串、双端队列都是序列容器,而设置、多集、地图、多重映射是标准关联容器),它底层使用平衡的搜索树 - 红黑树实现,插入删除操作时仅仅需要指针操作节点即可完成,不涉及到内存移动和拷贝,所以效率比较高.SET,顾名思义是“集合”的意思,在组中元素都是唯一的,而且默认情况下会对元素自动进行升序排列,支持集合的交(set_intersection),差(set_difference)并(set_union),对称差(set_symmetric_difference)等一些集合上的操作,如果需要集合中的元素允许重复那么可以使用多集。

    <sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。注意,<sstream>使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。

    isalpha(s[i])    判断字符是否为英文字母,若为小写字母返回2,大写字母返回1,若不是字母返回0

    tolower(s[i])    将大写字母转换为小写字母

    stringstream ss(s)    创建存储s的副本的stringstream对象

    dict.insert(buf)    向set容器添加元素

  • 相关阅读:
    c语言中 fgetc函数、fputc函数实现文件的复制
    c语言 13-7 利用fgetc函数输出文件的字符数
    c语言 13-6 利用fgetc函数输出文件的行数
    c语言中fgetc函数:显示文件内容
    c语言 13-5
    c语言 获取程序上一次运行时间的程序
    hzwer模拟赛 虫洞
    LYDSY热身赛 escape
    bzoj2330 糖果
    繁华模拟赛 Vicent坐电梯
  • 原文地址:https://www.cnblogs.com/tanjiaqi/p/7531378.html
Copyright © 2011-2022 走看看