• Winter-2-STL-E Andy's First Dictionary 解题报告及测试数据


    use stringstream

    Time Limit:3000MS     Memory Limit:0KB 

    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

    题解:

            这道题最重要的就是stringstream的使用。

    1. 使用getline(cin,string)函数整行读取。之所以不直接读取,是因为两个单词间有标点符号时,会误读为一个单词,还是需要后续的处理。     

    2. 由于最后要求输出的是小写,也方便排序,那么每输入一行就进行标点变为空格,大写变为小写的变换。处理完之后,就需要用到stringstream(字符串流)类,头文件是<sstream>,例如stringstream ss,ss.clear()是将字符串流清空,ss.str(string)是将ss读入对象初始化为形参 string,那么执行ss>>str,那么执行 while(ss>>str[k])k++;就可以将不带空格的字符串一一读入,从而实现了单词之间的分离。

    3. ​排序之后,使用pre记录刚刚输出的字符串,如果当前字符串与pre相同,则是重复的,不输出。因为排序后相同的字符串是连在一块的,所以用这种方法就将重复的全部排除了。          

    以下是代码:

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <cctype>
    #include <sstream>
    using namespace std;
    string str[100000],s;
    stringstream ss;
    int k=0;
    int main(){
        //freopen("1.in","r",stdin);
        while(getline(cin,s)){
            int len = s.length();
            for(int i=0;i<len;i++){//将标点变为空格,大写变为小写。
                char ch = s.at(i);
                if(islower(ch))continue;
                if(isupper(ch))s.at(i)-='A'-'a';
                else s.at(i)=' ';
            }
            ss.clear();
            ss.str(s);
            while(ss>>str[k])k++;
        }
        sort(str,str+k);//排序
        string pre="";
        for(int i=0;i<k;i++){
            if(str[i]==pre)continue;//重复的不输出
            cout << str[i]<<endl;
            pre =str[i];
        }
    }
  • 相关阅读:
    用户(三)
    首页和token验证(二)
    项目初始化和登录退出(一)
    VSCode设置vue/react模板
    Git操作
    C#可视化程序设计第三章(1,2)
    C#可视化程序设计第二章(3,4)
    SQL数据库第五章
    C#可视化程序设计第一章
    SQL数据库第四章
  • 原文地址:https://www.cnblogs.com/gzdaijie/p/4298828.html
Copyright © 2020-2023  润新知