栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Andy’s First Dictionary(安迪的第一部词典)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Andy’s First Dictionary(安迪的第一部词典)

题目:

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.

 

你需要写一个程序去列出输入文本的所有单词,问题中的单词定义为连续的字母序列,大写或小写。只有一个字母的单词也要考虑。此外,你的程序必须CaSe InSeNsItIvE,例如,“Apple” “apple”  “APPLE”都被看作一个单词。

 

思路:

 

将字符全部转换成小写存入set中,由set自动排序并去重,再顺序输出即可。

其中判断大小写用到is alpha(),大写转化成小写用到tolower(),同时用到stringstream将单词存入set容器中。

stringstream 是 C++ 提供的另一个字符串型的输入输出流,和iostream 类似。在这里使用它可以用空格键来分割字符,从而将单词一个个存入set。

 

代码:

直接上标程:

#include
using namespace std;
const int mod=1e9+7;
setdict;
int main()
{
    string s,buf;
    while(cin>>s)
    {
        for(int i=0; i>buf)dict.insert(buf);
    }
    //遍历set
    for(set::iterator it=dict.begin(); it!=dict.end(); it++)
    {
        cout<<*it< 

 

 

 

 

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846707.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号