题意:

输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有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. Luogu P1144 最短路计数 【最短路】 By cellur925

    题目传送门 常规的最短路计数问题:注意有重边(重边不用理,看样例),自环(读入时过滤). 另外这个无向图没有权,其实可以直接bfs做,但考虑到以后带权的情况,按spfa走了. 水题被卡了三次(嘤嘤嘤 ...

  2. JAVA值传递和引用传递 以及 实参是否改变

    八大数据类型和String 在进行传递的时候  不会改变. 八大数据类型 public class parameterValue { //值传递 public static void main(Str ...

  3. 暑期训练狂刷系列——Lightoj 1084 - Winter bfs

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...

  4. 2、IO流的分类和IO流体系

  5. 转 叫板OpenStack:用Docker实现私有云

    http://www.cnblogs.com/alexkn/p/4239457.html 看到各大厂商的云主机,会不会觉得高大上?目前大公司的主流方案是OpenStack,比如某个公司的私有云

  6. scala学习笔记4:函数和闭包

    以下主要记录的是看完scala in programming这本书functions and closures(第八章)后的要点总结. 1,函数可以存在的地方:函数方法,嵌套函数. 2,关于funct ...

  7. AJPFX总结在循环中break与continue的区别

    相信刚学编程的人很容易被break,continue这两个关键词搞混淆了,两者都有跳出循环的意思,但是他们到底有什么区别呢?其实很简单,break是结束整个循环体,continue是结束当前这一单次循 ...

  8. 学JAVA第二十二天,StringBuffer的好处

    五一的假期今天就结束了,又要回来上课了. 今天就写一下StringBuffer的好处吧. StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象. 也就是说,我们平时用String ...

  9. Android学习笔记(十二) 线程

    Android中的线程和Java中的线程使用方法类似,参考(四)Java基础知识回顾 MainThread与WorkerThread UI相关的线程都运行在主线程(MainThread/UIThrea ...

  10. java debug源码完整版

    第一步:现在myeclipse或者eclipse中下载jad插件,将class文件翻译成java文件 点击下载安装 第二步:创建一个java工程,导出成jar包.jdk自带的jar包不包含debug ...