• I


    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. 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
    解题思路:题目的意思就是读取文本中每个单词(只由字母组成,其他字符作为分割单词的标志),大写全部改成小写,然后排序输出。做法:set排序和去重。stringstream类从一个string对象中读取字符串流(空格为字符串间的分界)。
    AC代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main(){
     4     string str,buf;
     5     set<string> se;se.clear();
     6     while(cin>>str){
     7         for(size_t i=0;i<str.size();++i){
     8             if(isupper(str[i]))str[i]+=32;//如果是大写字母,就变成小写字母
     9             if(!isalpha(str[i]))str[i]=' ';//如果不是字母,就变成空格,分割字符串
    10         }
    11         stringstream ss(str);//每次初始化为给定的string对象
    12         while(ss>>buf)se.insert(buf);//读取该字符串中的字符串流
    13     }
    14     for(set<string>::iterator it=se.begin();it!=se.end();++it)
    15         cout<<*it<<endl;
    16     return 0;
    17 }
  • 相关阅读:
    iOS开发技巧,细节
    一个PO审批的自动触发问题
    SAP Workflow(一)
    【S4 导数据】S/4HANA导数,只会LSMW远远不够(转)
    【MM】采购订单文本(二)
    【MM】 采购订单长文本(一)
    SAP Create Background Job定义后台作业
    No.595-Big Countries-(LeetCode之Database篇)
    设计模式之单例模式
    你一定能看懂的JDK动态代理
  • 原文地址:https://www.cnblogs.com/acgoto/p/9413716.html
Copyright © 2020-2023  润新知