题意:

输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有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. JAVA值传递和引用传递 以及 实参是否改变

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

  2. ORA-00845错误原因及解决办法

    故障现象: 用startup命令启动数据库时出现ora-00845错误提示: SQL> startup ORA-00845: MEMORY_TARGET not supported on thi ...

  3. shell 2 解析

    ---- shell 3 /home/oracle/utility/macro/call_autopurge_arch.sh Description: Call purge archive log f ...

  4. 205 Isomorphic Strings 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...

  5. android开发学习 ------- 弹出框

    这是一种方法,是我觉得简单易懂代码量较少的一种: /* 创建AlertDialog对象并显示 */ final AlertDialog alertDialog = new AlertDialog.Bu ...

  6. Java操作pdf: JarsperReport的简单使用

    在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...

  7. vue--组件中的自定义事件

    父组件通过props向子组件传递数据,子组件通过自定义事件向父组件传递信息. 在子组件中通过$emit触发事件,父组件在直接使用子组件的地方使用v-on(即@)来监听子组件触发的事件. 举例:(不知道 ...

  8. 使用一个CSS Class去给标签定义Style

    使用一个CSS Class去给标签定义Style 类是可重用的样式,可以添加到HTML元素. 下面是一个CSS类声明的例子: <style>   .blue-text {     colo ...

  9. HttpWebRequest 以及WebRequest的使用

    1.WebRequest的发送数据以及接收数据 class Program { static void Main(string[] args) { //创建一个实例并发送请求 HttpWebReque ...

  10. Git ---创建和切换分支

    ······································································"天下武功,唯快不破" git分支: g ...