当前位置:网站首页>题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
2022-06-28 20:27:00 【Love_Jacques】
紫书P112;set的应用;Andy’s First Dictionary(UVa10815);
题目大意:
输入一个文本(最多500行,每行最多200个字符,以EOF结尾),找出所有不同的单词,按照字典序从小到大输出以小写输出(一行一单词)。
Sample Input
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.
Sample Output
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
题目分析:
数据结构选择:统计出现过的单词,理所当然想到set;
算法设计:暴力模拟,时间O(n)最大100000;每次读入一串字符,for循环判断单个字符是否为字母,若是转换为小写,若不是,则存入已读入的字母构成的单词,重新开始读入下一个单词;
//dict为set集合,s为当前读入的字符串,now为当前已经存入的字母构成的单词
if(isalpha(s[i])){
now+=tolower(s[i]);
}else{
if(!now.empty()) dict.insert(now);
now.clear();
}
模块设计:定义与预处理–读入与初始化–处理字符串–输出–return 0;
代码:
#include<iostream>
#include<set>
#include<cctype>
#include<string>
using namespace std;
struct rule{
bool operator()(const string &a,const string &b){
return a<b;
}
};
set<string,rule> dict;
string s;
int main()
{
while(cin>>s)
{
string now;
now.clear();
for(int i=0;i<s.length();i++)
{
if(isalpha(s[i])){
now+=tolower(s[i]);
}else{
if(!now.empty()) dict.insert(now);
now.clear();
}
}
if(!now.empty()) dict.insert(now);
}
for(set<string>::iterator i=dict.begin();i!=dict.end();i++)//auto i=dict.begin();
cout<<*i<<endl;
return 0;
}
要点与细节总结:
- 代码中使用了字符串处理函数
isalpha()与tolower();isalpha():判断是否为字母;islower():判断是否为小写字母;isupper():判断是否为大写字母;tolower():转换为小写字母;toupper():转换为大写字母; - set的插入
insert()若遇到重复值则无效; - 代码中对于now的处理可以用输入流替代:遇到非字母转换为空格,之后对s进行流输入处理,需要加头文件
#include<sstream>;
for(int i=0;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);
- 代码中定义迭代器
set<string>::iterator i=dict.begin();可以用更为简洁的auto i=dict.begin()替换。
更新与2020.6.23
边栏推荐
- On the complexity of software development and the way to improve its efficiency
- Day88.七牛云: 房源图片、用户头像上传
- 软件watchdog和ANR触发memory dump讲解
- 方 差 分 析
- openGauss内核分析之查询重写
- Various types of long
- csdn涨薪技术-Selenium自动化测试全栈总结
- TcWind 模式設定
- Grep text search tool
- [graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!
猜你喜欢

csdn涨薪技术-Selenium自动化测试全栈总结

API 网关 Apache APISIX 助力雪球双活架构演进

Rsync remote synchronization

如何使用 DataAnt 监控 Apache APISIX

openGauss内核分析之查询重写

学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
![[try to hack] cobalt strike (I)](/img/2b/5d274078b7d7ebd05b7c6d9e020868.png)
[try to hack] cobalt strike (I)

Leetcode 36. Effective Sudoku (yes, once)

CSDN salary increase technology selenium automated test stack summary

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
随机推荐
Resilience4j retry source code analysis and retry index collection
3. integrate listener
Comparisonchain file name sort
Real number operation
TcWind 模式設定
Fix the simulator that cannot be selected by flutter once
[learning notes] Introduction to principal component analysis
管道 | 与重定向 >
2. integrate filter
ANR问题--相机相关的debug
Pyinstaller打包pikepdf失败的问题排查
rsync远程同步
2022 P cylinder filling test exercises and online simulation test
Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
2788.Cifera
[learning notes] factor analysis
Lucene构建索引的原理及源代码分析
[try to hack] cobalt strike (I)
ThreadLocal原理
1. integrate servlets