图书管理
哈希集合存书名,find查存在性
正文
// 原题:https://oj.yecheng.tv/p/T1456
// 题意:第一行 n,随后 n 行 "add 书名" 或 "find 书名"(书名可含空格),每个 find 输出该书名是否已存在。
// 思路:哈希表存书名;整行读入后按第一个空格切出指令与书名,add 插入、find 查询,大小写敏感直接比字符串。
// 复杂度:O(n×|s|) 时间 / O(n×|s|) 空间
// 易错点:书名含空格必须 getline 整行读,读完 n 后要先吃掉行尾换行;大小写不同视为不同书。
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if(!(cin>>n)) return 0;
string line;
getline(cin,line);
unordered_set<string> book;
book.reserve((size_t)n*2+1);
for(int i=0;i<n;i++){
if(!getline(cin,line)) return 0;
if(!line.empty()&&line.back()=='\r') line.pop_back();
string op,name;
size_t p=line.find(' ');
if(p==string::npos){
op=line;
}else{
op=line.substr(0,p);
name=line.substr(p+1);
}
if(op=="add"){
book.insert(name);
}else if(op=="find"){
cout<<(book.count(name)?"yes":"no")<<'\n';
}
}
return 0;
}
一本通·提高篇的其它内容
- 活动安排
- 种树
- 喷水装置
- 加工生产调度
- 智力大冲浪
- 数列极差
- 数列分段
- 线段
- 家庭作业
- 钓鱼
- 糖果传递
- 愤怒的牛
- Best Cow Fences
- 曲线
- 数列分段 II
- 扩散
- 灯泡
- 传送带
- 数的划分
- 生日蛋糕
- 小木棍
- Addition Chains
- 埃及分数
- 平板涂色
- 质数方阵
- 靶形数独
- 电路维修
- 魔板
- Knight Moves
- 棋盘游戏
- Keyboarding
- 移动玩具
- 山峰和山谷
- Oulipo
- Power Strings
- Seekthe Name, Seek the Fame
- Friends
- A Horrible Poem
- Beads
- Antisymmetry
- 门票
- 收集雪花
- 剪花布条
- Power Strings
- Radio Transmission
- OKR-Periods of Words
- 似乎在梦中见过的样子
- Censoring
- Phone List
- The XOR Largest Pair
- Nikitosh 和异或
- Immediate Decodability
- L 语言
- Secret Message 秘密信息
- 背单词
- The Xor-longest Path
- Keywords Search
- 玄武密码
- Censoring
- 单词