安迪的第一个字典(Andy's First Dictionary,Uva 10815)
输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单 词不区分大小写。
样例输入:
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)的更多相关文章
- [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary
1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...
- 安迪的第一个字典(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 ...
- 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 ...
- 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 ...
- 安迪的第一个字典 (Andy's First Dictionary,UVa10815)
题目描述: #include<iostream> #include<string> #include<set> #include<sstream> us ...
- 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 ...
- 5_3 安迪的第一个字典(UVa10815)<set的使用>
Andy 是个 8 岁的孩子,他梦想要制作一本他自己的字典. 这并不是一件容易的事,因为他认识的字还不是那么多. 他并不想要自己去想这本字典中该放哪些字,他有个聪明的主意. 就是拿出他最喜欢的一本故事 ...
- UVa 10815 安迪的第一个字典
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- stl的集合set——安迪的第一个字典(摘)
set就是数学上的集合——每个元素最多只出现一次,和sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符 以下代码测试set中无重复元素 #include<iostream&g ...
随机推荐
- HTML DOM createTextNode() 方法
实例 创建一个文本节点: var btn=document.createTextNode("Hello World"); 输出结果: Hello World 尝试一下 » HTML ...
- Go 语言的下一个大版本:Go 2.0 被安排上了(全面兼容1.X,改进错误处理和泛型这两大主题)
今年 8 月 Go 开发团队公布了 Go 2.0 的设计草案,包括错误处理和泛型这两大主题.现在备受瞩目的 Go 2.0 又有了新动向 —— 昨日 Go 开发团队在其官方博客表示,Go 2 已经被安排 ...
- hibernate 下载
https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/ http://sourceforge.net/p ...
- JLabel作为展现元素时需要注意的事项
如果没有内容,JLabel默认透明就无法作为点击区域了,所以为了让其可以响应鼠标事件需要设置 setOpaque(true) 这样就可以响应鼠标事件了 (吐槽一下,多年以前在大学做个web地图导航的网 ...
- Android编译详解之lunch命令 【转】
本文转载自: Android编译详解之lunch命令 (2012-10-08 10:27:55) 转载▼ 标签: it 分类: android内核剖析 Android的优势就在于其开源,手机和 ...
- ubuntu安装wine 安装QQ
安装最新版 wine sudo dpkg --add-architecture i386 sudo add-apt-repository ppa:wine/wine-builds sudo apt-g ...
- 4.7.3 Canonical LR(1) Parsing Tables
4.7.3 Canonical LR(1) Parsing Tables We now give the rules for constructing the LR(1) ACTION and GOT ...
- [POI2012]FES-Festival
https://www.zybuluo.com/ysner/note/1252538 题面 有一个数列\(\{a\}\).现给定多组限制,限制分成\(2\)类,第一类是\(a_x+1=a_y\),有\ ...
- 2-6 ES6常用语法
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
题目描述 We consider a positive integer perfect, if and only if it is equal to the sum of its positive d ...