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 ...
随机推荐
- Linux exec与文件描述符
看到好几篇文章讲述exec都是一知半解,所以我尽量说的清楚明白一些.本文首先讲述Linux文件描述符,然后是exec,最后举例说明exec I/O重定向及其用法. 概念:exec命令用于调用并执行指令 ...
- maven系列--settings.xml
安装maven,会有一份settings.xml文件,我们可以理解这份文件就是用来管理本地maven的一系列东西的.这里我先贴出一份完整的原始xml. <?xml version="1 ...
- Tomcat对Struts中的Action进行授权利
.应用的意义 在某些应用下,如果Action类执行的功能比较重要,可以对该Action类进行授权利以实现只有特定角色的用户能够访问,此时可以在struts-config.xml文件中进行配置 2. ...
- HTML学习——标签
1.</hr>效果: 2.<q>简短文本引用, <blockquote>长文本引用, 表象:为文本添加一个双引号,实:一个语义,引用别人的话语. 3.&nb ...
- Akamai CDN
Akamai CDN中的几个重要组件 mapping system 调度系统(映射client到edge cluster,进而到edge server) edge server platform 边缘 ...
- Win和Linux查看端口和杀死进程
title: Win和Linux查看端口和杀死进程 date: 2017-7-30 tags: null categories: Linux --- 本文介绍Windows和Linux下查看端口和杀死 ...
- java编码GBK的不可映射字符
编译java文件时,提示编码GBK的不可映射字符. 主要原因:windows 默认编码方式为GBK,用javac编译时,中文按照GBK解析,但是文件内容编码格式不是GBK. 解决:若编译单个文件指定编 ...
- js万亿级数字转汉字的封装方法
要求如图: 实现方法: function changeBillionToCN(c) { // 对传参进行类型处理,非字符串进行转换 if(typeof(c) != "string" ...
- 有关mysql的for update以及 死锁问题
一.先说锁的概念 锁级别: 1.行级锁: InnoDB引擎(也支持表级锁,默认是行级锁),开销大,加锁慢:会出现死锁.锁定粒度最小,发生锁冲突的概率最低,并发度最高. 2.表级锁:MylSAM引擎和M ...
- 浅谈ASP.NET配置文件加密
在刚刚完成的一个ASP.NET项目中,遇到了这么一个问题,项目部署到生产环境中时,领导要求项目中的配置文件(如web.config,app.config)中不能出现敏感字符,如:数据库连接,等等. 第 ...