题意:

输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有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中文字符乱码(GB2312,GBK,GB18030相关的问题)

      http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ [已解决]python中文字符乱码(GB2312,GB ...

  2. Triangular Pastures POJ - 1948

    Triangular Pastures POJ - 1948 sum表示木条的总长.a[i]表示第i根木条长度.ans[i][j][k]表示用前i条木条,摆成两条长度分别为j和k的边是否可能. 那么a ...

  3. Styles and Themens(4)android自定义主题时可使用的属性

    A list of the standard attributes that you can use in themes can be found at R.styleable.Theme. Cons ...

  4. SpringCloud开发学习总结(六)—— 结合注解的AOP示例

    面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP ...

  5. windows8.1专业版 关闭ie11总是已停止工作

    该问题通常原因: 1 系统重病毒: 2 系统和安装的软件不兼容导致. 解决方案: 1 杀毒更新至最新进行杀毒,仍未解决,重新安装系统: 2 目前身边人员多数属于该情况: 1 如安装了输入法.迅雷或其它 ...

  6. composer Failed to decode zlib stream 无法解码zlib流

    Win7 中安装 Composer (PHP) 国内有些网络不能访问美国的Composer官网,可访问 Composer 中文网 学习. 目标 可以在任何目录下的项目中执行 PHP composer. ...

  7. SQL表与表连接关系

    一.SQL连接方式 left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join :右连接,返回右表中所有的记录以及左表中连接字段相等的记录. inner j ...

  8. 让px单位自动转换为rem的方法

    开发工具:      编辑器:vscode;     css预处理器:less;(无具体要求): 步骤:   1. vscode安装cssrem插件:   2. 修改css插件的默认配置,其默认转换p ...

  9. ScrollView嵌套GridView,GridView显示不全

    最近开发有个需求是以A-Z的方式区分全国城市(搜索功能),并实现字母索引的功能(有点类似微信,不过比较坑的是用的是GridView, 并且GridView上面还有几个LinearLayout). 详细 ...

  10. 最实用解决tomcat startup.bat 一闪而过

    1.直接到tomcat 的解压路径中找到log日志,eg:D:\tomcat\apache-tomcat-7.0.73\logs 查看 catalina 这个日志文件,可以清除的定位错误原因:一般可能 ...