安迪的第一个字典(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 ...
随机推荐
- 下载、编译、运行android 7.1系统(ubuntu 16.0.4)【转】
本文转载自:http://blog.csdn.net/andywuchuanlong/article/details/53977710
- FileStream StreamWriter StreamReader BinaryReader
FileStream vs/differences StreamWriter? http://stackoverflow.com/questions/4963667/filestream-vs-dif ...
- iOS方法重写
在O-C中子类可以继承父类的方法 ,而不需要从新编写相同的方法,但是有有时候子类并不想原封不动的继承父类的方法,而且是想做一些修改,这就采用啦方法的重写,方法从写有叫做方法覆盖,若子类的中的方法与父类 ...
- UVA 315 求连通图里的割点
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 哎 大白书里求割点的模板不好用啊,许多细节理解起来也好烦..还好找了 ...
- python-----tuple用法
有一种有序列表叫元组:tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样是列出同学的名字: >>> classmates = ('Michael' ...
- pyhon-----安装yaml踩过的坑以及正解
之前在网上找了各种资料,cmd安装yaml,网上大部分写的都是pip install yaml 可是,执行完就变成Could not find a version that satisfies the ...
- gitlab-ce平台调试
SMTP使用QQ exmail 遇到的问题:使用qinrui@easyunion.net对516059158@qq.com能发送验证邮件,但是无法打开验证连接
- Python 返回多个值+Lambda的使用
def MaxMin(a,b): if(a>b): return a,b else: return b,a max,min=MaxMin(8,95) print "最大值为:" ...
- 2008提权之突破系统权限安装shift后门
大家都知道08权限的系统权限设置很严格,且在2003系统中常用到的溢出工具都失效.面对限制IP连接的情况 我们及时拿到system权限 有账号也上不去 这种情况下只能弄shift后门 或者放大镜了.但 ...
- MySQL 启动服务和登陆参数
启动MySQL服务:net start mysql; 停止MySQL服务:net stop mysql; 参数 描述 -D,--database=name 打开指定数据库 --delimiter=na ...