Andy’s First Dictionary( stringstream )

简介: Andy’s First Dictionary( stringstream )

1.描述:


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.

8岁的安迪有一个梦想——他想制作自己的字典。这对他来说不是一项容易的任务,因为他知道的单词数量还不够。他没有自己想出所有的单词,而是有了一个聪明的想法。他会从书架上挑选一本他最喜欢的故事书,从中抄写所有不同的单词。按字母顺序排列单词,他就完成了!当然,这是一项非常耗时的工作,而这正是计算机程序有用的地方。


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.

你被要求编写一个程序,列出输入文本中的所有不同单词。

在这个问题中,单词被定义为字母的连续序列,大写和/或小写。只有一个字母的单词也要考虑。此外,您的程序必须不区分大小写。例如,像“Apple”、“apple”或“APPLE”这样的词必须被认为是相同的。


2. 输入:


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.


3.输出:


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.


4.样例输入:


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.


5.样例输出:


a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when


6.题目大意:


给出几行句子,找出所有不同的单词,按字典序排序输出


7.思路


用set储存单词,自动去重和排序,用stringstream处理句子,先去掉各种符号并处理成小写,再存入set


8.代码


#include<bits/stdc++.h>
using namespace std;
string s;
set<string>se;
int main()
{
  while(getline(cin,s))
  {
    int len=s.size();
    for(int i=0;i<s.size();i++)
    {
      if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
      {
        s[i]=tolower(s[i]);
      }//是字母小写
      else
      {
        s[i]=' ';
      }//是标点转换成空格
    }
    stringstream ss(s);
    while(ss>>s)
    {
      se.insert(s);
    }//存入set
  }
  for(auto k : se)
  {
    cout<<k<<endl;
  }//输出
}


9.反思


一定要注意标点符号对于单词的影响,要先处理掉标点再使用 stringstream 操作


目录
相关文章
【Java每日一题,字符串正则匹配】Andy‘s First Dictionary
【Java每日一题,字符串正则匹配】Andy‘s First Dictionary
UVA10815 安迪的第一个字典 Andy‘s First Dictionary
UVA10815 安迪的第一个字典 Andy‘s First Dictionary
|
6月前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
78 3
|
存储 Java Python
多重字典(Multi-Level Dictionary)
多重字典(Multi-Level Dictionary)是一种将多个字典组合在一起的数据结构,用于解决需要在多个维度上查找数据的问题。多重字典可以看作是一个嵌套的字典,每个字典都可以作为其他字典的键。 使用多重字典的场景:
166 3
|
6月前
|
存储 缓存 数据库连接
Python基础教程——字典(Dictionary)
Python基础教程——字典(Dictionary)
|
5月前
|
存储 Python 容器
|
5月前
|
Python 存储 容器
Python 字典(Dictionary)
Python 字典(Dictionary)
|
6月前
|
开发者 Python
【Python 基础】递推式构造字典(dictionary comprehension)
【5月更文挑战第8天】【Python 基础】递推式构造字典(dictionary comprehension)
|
6月前
|
存储 数据处理 Python
Python中的字典(Dictionary)类型:深入解析与应用
Python中的字典(Dictionary)类型:深入解析与应用
62 0
|
6月前
|
存储 算法 数据库
Python字典(Dictionary)
Python字典(Dictionary)
54 0