UVa230 Borrowers
原题链接 UVa230
思路
这题输入时有一些字符串处理操作,可以利用string的substr()函数和find_last_of()函数更加方便,处理时不必更要把书名和作者对应下来,注意到原题书名的输出是带有双引号的,而且作者只是用来排序,那么可以直接把输入截断成为标题和作者名。例如:输入 “The Canterbury Tales” by Chaucer, G. 直接将“The Canterbury Tales”作为书名,将字符串 by Chaucer, G.作为作者名,这样做并不会影响排序,而且处理起来特别方便。
这题可以把书的信息写为一个结构体,用map把书名和结构体做一个映射就可以很方便的查询书的状态了,用一个vector保存要归还的书籍的名称。
struct book
{
string author; //作者
int tag; //1代表在架上、0代表被借、-1代表归还
};
map <string,book> books;
vector <string> name; //书名
必须先给name排序,排序要调用的函数如下
bool cmp(string a,string b)
{
if(books[a].author<books[b].author)
return true;
else if(books[a].author==books[b].author&&a<b)
return true;
return false;
}
对于”BORROW”指令,令书名对应的状态成为0;
对于”RETURN”指令,令书名对应的状态为-1;
最后在处理”SHELVE”指令时,利用一个for从vector的头到尾遍历,如果书名对应的状态是-1,即归还的,那么就查找它的前一个在架的书名,如果前一个不存在就说明把它放在最前面;
AC代码
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
struct book
{
string author;
int tag; //1代表在架上、0代表被借、-1代表归还
};
map <string,book> books;
vector <string> name;
bool cmp(string a,string b)
{
if(books[a].author<books[b].author)
return true;
else if(books[a].author==books[b].author&&a<b)
return true;
return false;
}
int main()
{
string input;
book writer;
while(getline(cin,input)&&input!="END")
{
string temp=input.substr(0,input.find_last_of("\"")+1);
name.push_back(temp);
writer.author=input.substr(input.find_last_of("\"")+1);
writer.tag=1;
books[temp]=writer;
}
sort(name.begin(),name.end(),cmp);
string cmd,names;
while(cin>>cmd&&cmd!="END")
{
if(cmd[0]=='S')
{
for(int i=0;i<name.size();i++)
{
if(books[name[i]].tag==-1)
{
int j;
books[name[i]].tag=1;
for(j=i-1;j>=0;--j)
{
if(books[name[j]].tag==1)
break;
}
if(j==-1)
cout<<"Put "<<name[i]<<" first"<<endl;
else cout<<"Put "<<name[i]<<" after "<<name[j]<<endl;
}
}
cout<<"END"<<endl;
}
else
{
getchar();
getline(cin,names);
if(cmd[0]=='B')
{
books[names].tag=0;
}
else if(cmd[0]=='R')
{
books[names].tag=-1;
}
}
}
return 0;
}
如有错误欢迎指出!!
UVa230 Borrowers的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers
//又开学啦,不知不觉成为大二的老人了...时间过得好快啊,感觉好颓废... 题意:建立一个借书/归还系统.有借.还.把还的书插到书架上这三个指令. 代码:(Accepted, 0ms) //UVa2 ...
- UVa230 Borrowers (STL)
Borrowers I mean your borrowers of books - those mutilators of collections, spoilers of the symmet ...
- uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!
I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ...
- UVa 230 Borrowers(map和set)
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelve ...
- Borrowers
Description I mean your borrowers of books - those mutilators of collections, spoilers of the symmet ...
- Borrowers UVA - 230
I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...
- Uva - 230 - Borrowers
AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ...
- UVA 230 Borrowers (STL 行读入的处理 重载小于号)
题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...
- 【习题 5-8 UVA - 230】Borrowers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map+set写个模拟就好. 3个区域 书架.桌子.别人的手上. 其中前两个区域的书都能借出去. [代码] #include &l ...
随机推荐
- HTML学习(一)
文本输出/超链接 <!--/* * @<h1></h1>到<h6></h6>六个h标签,分别表示不同大小的字体.h1最大,h6最小 * @< ...
- Tomcat--startup.bat文件
Tomcat--startup.bat文件 如何启动tomcat,如何关闭tomcat等常规操作,我们应该都很清楚了,但是实际中我们经常会遇到一些恶心的情景,比如说正在我们撸码撸的很高兴的时候,ecl ...
- Spark 读写hive 表
spark 读写hive表主要是通过sparkssSession 读表的时候,很简单,直接像写sql一样sparkSession.sql("select * from xx") 就 ...
- vmware虚拟机无法连接网络
这是一个老生常谈的问题,而且网上有一套解决方法,最方便快捷的肯定属恢复虚拟网络了 说说我的情况 虚拟机VMware® Workstation 12 Pro centos6.8,克隆,192.168.2 ...
- 【转】globk中的卫星轨道约束
在globk中使用轨道随机参数很灵活,允许对每一期测量的每个卫星的每个轨道根数使用不同的马尔科夫约束.但是困难的是如何选择有效客观的约束级别.通常是选择不同的值进行实验以得到理想值.下面是三种不同的约 ...
- nagios的安装
Nagios通常由一个主程序(Nagios).一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE.NSCA. NSClient++和NDOUtils)组成.Nagios的监控 ...
- useradd和adduser
1.Ubuntu中,adduser是一个脚本,而useradd是一个二进制程序,前者对后者进行了封装,更加智能. 2.Centos中,adduser和useradd完全相同,adduser是一个符号链 ...
- C语言预处理 编译 汇编 链接四个阶段
c程序(源代码)转换成可以在硬件上运行的程序(可执行代码),需要进行编译和链接. 编译过程 编译过程又可以分成两个阶段:编译和会汇编. 编译 编译是读取源程序(字符流),对之进行词法和语法的分析,将高 ...
- 基于tomcat+springMVC搭建基本的前后台交互系统
一.摘要 1.所需软件列表: 1) tomcat : apache-tomcat-7.0.54 服务端容器 2) Intellij: Intellij IDEA 14.0.3 开发 ...
- mui点击加载,下拉刷新,上下整合代码
mui点击加载,下拉刷新,上下整合代码 mui的是上拉加载,但是老大说要做成点击加载,所以就改了一些 代码应该是有些问题的,测到了大家就自己改下. 首先要说明的是,有下拉刷新的页面一定要是双webvi ...