输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单 词不区分大小写。

样例输入:

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

【分析】

本题没有太多的技巧,只是为了展示set的用法:由于string已经定义了“小于”运算符, 直接使用set保存单词集合即可。注意,输入时把所有非字母的字符变成空格,然后利用 stringstream得到各个单词。

#include <iostream>
#include<string>
#include<set>
#include<sstream>
using namespace std; set<string> dict; //string 集合 int main(){
string s,buf;
while(cin>>s){
for(int i=;i<s.length();i++){
if(isalpha(s[i])) s[i]=tolower(s[i]);else s[i]=' ';
}
stringstream ss(s);
while(ss>>buf ) dict.insert(buf); }
for(set<string>::iterator it = dict.begin();it!=dict.end();++it){
cout<<*it<<"\n";
}
return ;
}

上面的代码用到了set中元素已从小到大排好序这一性质,用一个for循环即可从小到大 遍历所有元素。iterator的意思 是迭代器,是STL中的重要概念,类似于指针。和“vector类似于数组”一样,这里的“类似”指 的是用法类似。

安迪的第一个字典(Andy's First Dictionary,Uva 10815)的更多相关文章

  1. [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary

    1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...

  2. 安迪的第一个字典(Andy's First Dictionary,UVa 10815)

    Description Andy, , has a dream - he wants to produce his very own dictionary. This is not an easy t ...

  3. STL语法——集合:set 安迪的第一个字典(Andy's First Dictionary,UVa 10815)

    Description Andy, , has a dream - he wants to produce his very own dictionary. This is not an easy t ...

  4. Andy‘s First Dictionary UVA - 10815

      Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for h ...

  5. 安迪的第一个字典 (Andy's First Dictionary,UVa10815)

    题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...

  6. set的运用 例题5-3 安迪的第一个字典(Andy's First Dictionary,Uva 10815)

    #include<bits/stdc++.h>using namespace std;set<string> dict;int main(){ string s, buf; w ...

  7. 5_3 安迪的第一个字典(UVa10815)<set的使用>

    Andy 是个 8 岁的孩子,他梦想要制作一本他自己的字典. 这并不是一件容易的事,因为他认识的字还不是那么多. 他并不想要自己去想这本字典中该放哪些字,他有个聪明的主意. 就是拿出他最喜欢的一本故事 ...

  8. UVa 10815 安迪的第一个字典

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. stl的集合set——安迪的第一个字典(摘)

    set就是数学上的集合——每个元素最多只出现一次,和sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符 以下代码测试set中无重复元素 #include<iostream&g ...

随机推荐

  1. Error处理: android.media.MediaRecorder.start(Native Method) 报错:start failed: -19【转】

    本文转载自:http://blog.csdn.net/netwalk/article/details/17686993 Error处理: android.media.MediaRecorder.sta ...

  2. leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  3. MSD3458开发资料

    MSD3458HB是mstar最新推出的普通4K数字电视解决方案,不带操作系统,是一个低成本的4K方案,156脚的LQFP普通封装,开发难度低,支持HDMI2.0输入,支持4K输出.1. Suppor ...

  4. gitlab smtp设置

    QQ exmail gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "smtp.exmail.qq.com ...

  5. android View页面布局总结 最全总结(转)

    下面是我在工作中总结的内容,希望对大家有帮助. 一.布局 View的几种布局显示方式有下面几种:线性布局(LinearLayout).相对布局(RelativeLayout).表格布局(TableLa ...

  6. NTFS中的ADS的一些问题

    有关ADS的简单说明请看http://www.xfocus.net/articles/200212/466.html 可以看到ADS在很久以前就被一些安全人员所关注,并且也提出了一些经典的利用,比如隐 ...

  7. 近年来火热的人工智能,其实是IT业界的一个障眼法

    近年来火热的人工智能,其实是IT业界的一个障眼法,仗着现在的计算机的计算能力牛B,把一个类仿生统计算法,宣传成了人工智能,不得不感叹一些营销人士的牛逼,说大话不腰疼.当然谎言重复一千遍也许自己也就信了 ...

  8. E20171108-hm

    breadcrumb  n. 面包屑:面包心; 2.面包的松软(或柔软)部分;                    n.  网页导航区(a -> b -> c)

  9. tfs

    安装Team Foundation Server 2012过程截图 专题图 1,下载Team Foundation Server 2012  官方下载: http://www.microsoft.co ...

  10. [POJ3417]Network/闇の連鎖

    Description 传说中的暗之连锁被人们称为 Dark. Dark 是人类内心的黑暗的产物,古今中外的勇者们都试图打倒它.经过研究,你发现 Dark 呈现无向图的结构,图中有 N 个节点和两类边 ...