//又开学啦,不知不觉成为大二的老人了。。。时间过得好快啊,感觉好颓废。。。

题意:建立一个借书/归还系统。有借、还、把还的书插到书架上这三个指令。


代码:(Accepted, 0ms)

//UVa230 - Borrowers
#include<iostream>
#include<string>
#include<map>
#include<set>
using namespace std; struct BOOK {
string au, ti;
BOOK(const string& a, const string& t) :au(a), ti(t) {}
BOOK() :ti("X"), au("X") {}
bool operator <(const BOOK &that)const {
if (au == that.au) return ti < that.ti;
return au < that.au;
}
};
map<BOOK, bool> list;//int:0在书架 1已经借出
map<string, BOOK> auth;
set<BOOK> ret;//归还列表
string tmp; void B(const string& t) {
list[auth[t]] = true;
} void R(const string& t) {
auto f = list.find(auth[t]);
ret.insert(f->first);
} void S() {
for (auto& r : ret) {
cout << "Put " << r.ti;
auto re = list.find(r),p=re;
//if (re == list.end()) cout << "FUCK!\n";//
while (p != list.begin() && (--p)->second);
//cout << "喵喵喵?????\n";//
if (p == list.begin() && p->second) cout << " first\n";
else cout << " after " << p->first.ti << '\n';
re->second = false;
}
cout << "END\n";
ret.clear();
} int main()
{
//freopen("in.txt", "r", stdin);
while (getline(cin, tmp) && tmp[0] != 'E') {
size_t n = tmp.rfind('\"');
string ti(tmp.substr(0, n + 1));
string au(tmp.substr(n + 5, tmp.size() - n - 6));
list[BOOK(au, ti)] = false;
auth[ti] = BOOK(au, ti);
}
//for (auto&r : list) cout << "--" << r.first.ti << '\t' << r.first.au << endl;//
while (getline(cin, tmp) && tmp[0] != 'E') {
if (tmp[0] == 'B') B(tmp.substr(7));
else if (tmp[0] == 'R') R(tmp.substr(7));
else S();
}
return 0;
}

分析:使用了STL的map和set。一个map存放所有书(书的作者与标题作为key,借阅/归还状态为value),一个map用来从标题映射到书的信息(因为下面borrow和return指令里不带作者信息,映射一下方便,但我内心还是不大情愿。的确有更好的方法,我不大会,下面再讲。)还有一个set存储归还了但还未放回书架的书。其他没啥要说的。

本来想将key设为标题,value设为作者的,但是不知道如何将map按value排序。查了查要用到vector+pair 中转,好像有点烦啊。但是网上看到这个,http://blog.csdn.net/a197p/article/details/43747539 。他就是采用了

struct book{
string author;
int status;
};
map<string, book> books;

来存储。我还以为他能给map按照value重排,结果他建立了个vector< string > name; 来存放标题,存放顺序用以下方法

bool compare(string a, string b){
if(books[a].author == books[b].author) return a < b;
else return books[a].author < books[b].author;
}

原来是稍微绕了一下,但是也不错耶。

[刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...

  2. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

  3. [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A

    题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...

  4. [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...

  5. [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation

    题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...

  6. [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile

    题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...

  7. [刷题]算法竞赛入门经典(第2版) 6-1/UVa673 6-2/UVa712 6-3/UVa536

    这三题比较简单,只放代码了. 题目:6-1 UVa673 - Parentheses Balance //UVa673 - Parentheses Balance //Accepted 0.000s ...

  8. [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities

    题意:模拟患者做手术. 其条件为:医院有Nop个手术室.准备手术室要Mop分钟,另有Nre个恢复用的床.准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟.现在医院 ...

  9. [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

    题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...

  10. [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web

    题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...

随机推荐

  1. benchmark 库

    性能测试的库 https://github.com/bestiejs/benchmark.js

  2. kvm的sshd起不来

    同事不知道在一台KVM虚拟机执行了chmod -R 777 /  将更目录中的所有文件的权限都改为777,重启该虚拟机之后发现该虚拟机登不上去了,来找我,我尝试重启了几次虚拟机之后,发现总是可以被pi ...

  3. [SinGuLaRiTy] 2017-03-27 综合性测试

    [SinGuLaRiTy-1013] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 这是 三道 USACO 的题...... 第一题:奶牛飞 ...

  4. .net做的exe和electron做的exe之间的通信问题

    目前工作遇到个问题: .net做的exe和electron做的exe,之间进行数据通信 目前找到两个相对方便的方法: 1.命名管道 ①.net命名管道资料: 进程间通信 - 命名管道实现 ②elect ...

  5. CSS3学习笔记(4)-CSS3函数

    p{ font-size: 15px; text-indent: 2em; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid ...

  6. Redis基础学习(一)—Redis的安装

    一.Redis的安装 1.在Linux环境下安装gcc环境 yum install gcc-c++   2.解压缩Redis源码包 tar -zxf redis-3.0.0.tar.gz   3.编译 ...

  7. jquery转盘抽奖的研究

    先看效果: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...

  8. 使用canvas实现擦除效果

    HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像.画布是一个矩形区域,您可以控制其每一像素.canvas 拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. html ...

  9. 【转】JDBC学习笔记(5)——利用反射及JDBC元数据编写通用的查询方法

    转自:http://www.cnblogs.com/ysw-go/ JDBC元数据 1)DatabaseMetaData /** * 了解即可:DatabaseMetaData是描述数据库的元数据对象 ...

  10. CF #284 div1 D. Traffic Jams in the Land 线段树

    大意是有n段路,每一段路有个值a,通过每一端路需要1s,如果通过这一段路时刻t为a的倍数,则需要等待1s再走,也就是需要2s通过. 比较头疼的就是相邻两个数之间会因为数字不同制约,一开始想a的范围是2 ...