题意:

输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE。

BORROW 和 RETURN 都会带有一个书名在后面,如:

BORROW "The Canterbury Tales"
RETURN "The Canterbury Tales"

当遇到SHELVE指令,输出已还但没上架的书排序后(规则同上)依次插入书架的序列。

用例:

输入:

"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "The Canterbury Tales"
RETURN "The Canterbury Tales"
BORROW "The C Programming Language"
RETURN "The C Programming Language"
SHELVE
BORROW "Algorithms"
RETURN "Algorithms"
SHELVE
END

输出:

Put "The Canterbury Tales" first
Put "The C Programming Language" after "The Canterbury Tales"
END
Put "Algorithms" after "The C Programming Language"
END

分析:

这题的读入有点麻烦,我的处理是使用getline, 然后将 “ 号变成空格 将空格变成“#”(ASCII差距不大), 再用stringsteam输入到string中,最后输出再将“#”转换为“ ”。

网上的方法是使用string.substr()截取,用string.find()去寻找位置, 我截取这种方法更好(stringsteam很慢)。

用map<string,int>将string映射到书架位置上面,用一个bool去标记这个位置有没被拿走,注意return后不能直接把书放回去,要标记,等收到SHELVE指令再排序放书。

 #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <set>
#include <sstream>
#include <algorithm>
using namespace std;
struct B
{
string name;
string author;
B(){}
B(const string& _name,const string& _author):
name(_name),author(_author){}
friend bool operator < (const B a,const B b)
{
if(a.author < b.author) return true;
else
{
if(a.author == b.author)
{
if(a.name < b.name)
return true;
else return false;
}
else return false;
}
}
};
struct A
{
int a;
int b;
A(){}
A(int _a, int _b):
a(_a), b(_b){}
// a after b of a is the first(b=-1);
};
typedef set<B> :: iterator sIT;
typedef vector<B> :: iterator vIT;
typedef vector<A> :: iterator aIT;
typedef vector<int> :: iterator IT;
string trans(const string& a)
{
string t = a;
for(int i = ; i < t.size(); i++)
{
if(t[i] == '#') t[i] = ' ';
}
return t;
}
int main()
{
ios::sync_with_stdio(false);
#if LOCAL
freopen("1.txt","r",stdin);
freopen("2.txt","w",stdout);
#endif // LOCAL
string s;
vector<B> book;
bool lend[];
vector<A> ans;
fill(lend, lend+,);
map<string,int> id;
vector<int> ret;
while(getline(cin,s))
{
for(int i = ; i < s.size(); i++)
{
if(s[i] == '"') s[i] = ' ';
else if(s[i] == ' ') s[i] = '#';
}
stringstream ss(s);
string tname, tauthor;
if(!(ss>>tname>>tauthor)) break;
book.push_back(B(tname,tauthor));
}
sort(book.begin(), book.end());
int cnt = ;
for(vIT it = book.begin(); it != book.end(); it++)
{
id[(*it).name] = cnt++;
}
string op;
while(cin>>op)
{
if(op=="END") break;
if(op == "SHELVE")
{
sort(ret.begin(), ret.end());
for(IT it = ret.begin(); it != ret.end(); it++)
{
lend[*it] = ;
int c = (*it) -;
for(; c >= && lend[c] == ; c--);
ans.push_back(A(*it,c));
}
ret.clear();
for(aIT it = ans.begin(); it != ans.end(); it++)
{
if((*it).b != -)
{
cout<<"Put \""<< trans(book[(*it).a].name) << "\" after \"" << trans(book[(*it).b].name) << "\"\n"; }
else
{
cout<<"Put \""<<trans(book[(*it).a].name)<< "\" first"<<"\n"; }
}
ans.clear();
cout<<"END\n";
continue;
}
cin.ignore();
string b;
getline(cin,b);
for(int i = ; i < b.size(); i++)
{
if(b[i] == '"') b[i] = ' ';
else if(b[i] == ' ') b[i] = '#';
}
stringstream ss(b);
ss>>b;
if(op== "BORROW")
{
lend[id[b]] = ;
}
else if(op == "RETURN")
{ ret.push_back(id[b]);
}
} return ;
}

UVA 230 Borrowers (STL 行读入的处理 重载小于号)的更多相关文章

  1. uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!

    I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ...

  2. UVa 230 Borrowers(map和set)

    I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelve ...

  3. Uva - 230 - Borrowers

    AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ...

  4. Java-map-第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

  5. string行读入&&文件输入

    普通读入的时候会以空格作为分隔符 直接用cin>>s读入,此时可以直接处理文件尾的情况 text代码: #include <iostream>#include <cstd ...

  6. Borrowers UVA - 230

      I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shel ...

  7. 【习题 5-8 UVA - 230】Borrowers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map+set写个模拟就好. 3个区域 书架.桌子.别人的手上. 其中前两个区域的书都能借出去. [代码] #include &l ...

  8. UVa230 Borrowers (STL)

     Borrowers  I mean your borrowers of books - those mutilators of collections, spoilers of the symmet ...

  9. C/C++如何整行读入字符串?

    在写代码的过程中,经常要一行一行的读入字符串进行处理,这里我总结了一些方法,供大家参考. 方法一:scanf()读入char[] 使用方法: ]; scanf("%[^\n]",& ...

随机推荐

  1. 升级Python后, yum不能用了

    yum需要使用python2,而升级python3后,就会导致语法错误 更正方法: sudo vim /usr/bin/yum 然后把第一行的python改成python2就好了 之后如果出现类似的, ...

  2. 原生JavaScript之实战 模拟重力场(篮球)

    成品图如下所示: 点击篮球让篮球掉下 搭建HTML+CSS代码 html: <div id="demo"></div> css: div{ width:10 ...

  3. 【洛谷4769】[NOI2018] 冒泡排序(动态规划_组合数学)

    题目: 洛谷 4769 博客页面左下角的嘴嘴瓜封神之战中的题目 分析: 一个排列交换次数为 \(\frac{1}{2}\sum_{i=1}^{n}|i-p_i|\) 的充要条件是这个排列不存在长度为 ...

  4. Java中的流(5)大数据流的分段读取

    来自文件 或 网络的InputStream数据量可能很大,如果用流的大小申请byte[],可能内存不足报错. 解决方案:分段读取 InputStream的方法int available()返回本次可读 ...

  5. Access OLE对象和附件的区别

    OLE 对象 来自 Office 和基于 Windows 的程序的图像.文档.图形和其他对象 最多可存储 2GB 数据(此大小限制适用于所有 Access 数据库).请记住,添加 2GB 数据会导致数 ...

  6. React.js 简介

    React.js 是一个帮助你构建页面 UI 的库.如果你熟悉 MVC 概念的话,那么 React 的组件就相当于 MVC 里面的 View.如果你不熟悉也没关系,你可以简单地理解为,React.js ...

  7. 学JAVA第二十三天,List类型和Set类型

    数组,是我们最常用的,但是有时候,我们要用数组,但是又不知道数组的类的长度的时候, 我们java就有一个很好用的工具Collection,这都是java的爸爸的用心良苦,Collection中包含Li ...

  8. 学习笔记 第十四章 使用CSS3动画

    第14章   使用CSS3动画 [学习重点] 设计2D动画 设计3D动画 设计过渡动画 设计帧动画 能够使用CSS3动画功能设计页面特效样式 14.1  设计2D动画 CSS2D Transform表 ...

  9. 在action中将字符串、对象、list集合保存到值栈中,在jsp页面中获取的方法

    转自:csdn 封装对象User,属性有id,username,email等1.1:在action中将字符串保存到值栈中   1.1.1 获取值栈对象         ValueStack stack ...

  10. Xcode 9 打印信息解决

    Xcode 9 打印信息解决 打印信息 1 nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNe ...