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

样例输入:

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. MFC ListControl技巧汇总

    转自:http://hi.baidu.com/qi_xian/blog/item/1971aa22da89ada24723e856.html 以下未经说明,listctrl默认view 风格为repo ...

  2. POJ 1330 LCA裸题~

    POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...

  3. 分布式消息中间件Rabbit Mq的了解与使用

    MQ(消息队列)作为现代比较流行的技术,在互联网应用平台中作为中间件,主要解决了应用解耦.异步通信.流量削锋.服务总线等问题,为实现高并发.高可用.高伸缩的企业应用提供了条件. 目前市面比较流行的消息 ...

  4. C#面向过程项目之飞行棋

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 飞行棋V ...

  5. bzoj 1598: [Usaco2008 Mar]牛跑步【A*K短路】

    A*K短路模板,详见https://blog.csdn.net/z_mendez/article/details/47057461 算法流程: 把有向图全建成反向边,跑一遍所有点到t的最短路记为dis ...

  6. HTML5常用知识点

    github代码:https://github.com/showkawa/H5C3/tree/master/html5 1.自定义属性 data- 1.1 可以给html里的所有DOM对象都可以添加一 ...

  7. HUE通过oozie工作流执行shell脚本

    HUE通过oozie工作流执行shell脚本 2018年01月17日 16:20:38 阅读数:217 首先上传对应的jar包和storm.sh脚本到hdfs,脚本内容如下: 脚本主要内容是:从hdf ...

  8. python自动化学习笔记11-自动化测试UTP框架

    前面基本的unittest及ddt已经学过了,现在我们系统把这些知识结合起来,写一个简单的UTP自动化测试框架: 我们先来建基础目录,首先新建一个项目,项目下建父目录UTP,conf目录,用来存放配置 ...

  9. ASP.Net 知识点总结(四)

    1.get和post 的区别 get是从服务器上获取数据,post是向服务器传送数据; get安全性非常低,数据显示在地址栏,post安全性较高: 但是执行效率却比Post方法好: get有字节限制为 ...

  10. C# 事件与委托(转载)

    委托的定义 delegate 是 C# 中的一种类型,它实际上是一个能够持有对某个方法的引用的类.与其它的类不同,delegate 类能够拥有一个方法的签名(signature),并且它"只 ...