原题链接 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的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 5-8/UVa230 - Borrowers

    //又开学啦,不知不觉成为大二的老人了...时间过得好快啊,感觉好颓废... 题意:建立一个借书/归还系统.有借.还.把还的书插到书架上这三个指令. 代码:(Accepted, 0ms) //UVa2 ...

  2. UVa230 Borrowers (STL)

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

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

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

  4. UVa 230 Borrowers(map和set)

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

  5. Borrowers

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

  6. Borrowers UVA - 230

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

  7. Uva - 230 - Borrowers

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

  8. UVA 230 Borrowers (STL 行读入的处理 重载小于号)

    题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...

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

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

随机推荐

  1. SCOPE_IDENTITY()

    @@IDENTYITY,SCOPE_IDENTITY的主要区别:在有触发器中而且触发器的内容里面含有插入标识符的操作的时候,@@IDENTITY则返回的是触发器里面新插入标识符的值而SCOPE_IDE ...

  2. MySQL查询结果复制到新表(更新、插入)

    MySQL中可以将查询结果复制到另外的一张表中,复制的话通常有两种情况,一种是更新已有的数据,另一种是插入一条新记录.下面通过例子来说明.首先构建两个测试表. 表t1: 表t2: 1.如果t2表中存在 ...

  3. linux socket 编程(C语言)[转]

    最近看了一些网络编程的书籍,一直以来总感觉网络编程神秘莫测,其实网络编程入门还是很容易学的,下面这些代码是我在linux下编写的,已经运行过了,编译之后就可以运行了.有不足之处希望大家多多指出,共同学 ...

  4. 怎样共享windows和linux之间的文件

    注:本文参考自:https://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/,相当于是原文的翻译. 一. ...

  5. Centos7下Java开发基本环境搭建

    一.Centos7安装JDK 首先查看自己的机器上是否已经自带openjdk,命令如下: rpm -qa | grep jdk 如果存在,则按照如下命令进行依次卸载: yum -y remove fi ...

  6. Apache Traffic Server服务搭建

    一.简介 Apache Traffic Server(ATS或TS)是一个高性能的.模块化的HTTP代理和缓存服务器,与 Nginx 和 Squid 类似.它通过将频繁访问的信息缓存在网络的边缘来改善 ...

  7. 编译和解释性语言和python运行方式

    1.编译型语言和解释性语言 编译型语言:在执行之前需要一个专门的编译过程,把程序编译成为机器语言的文件,运行时不需要重新翻译,直接使用编译的结果就行了.程序执行效率高,依赖编译器,跨平台性差些.如C. ...

  8. spring之AspectJ基于xml AOP编程

    一.引言: AspectJ框架不仅实现了面向切面编程,而且还支持注解,spring将它引入自己的规范之中. 二.需要了解: AspectJ是基于java语言的AOP框架 spring2.0之后支持As ...

  9. maven项目引入sqljdbc4 找不到包的完美 解决方案。

    今天碰到了这个问题,解决了,顺便做一下记录.首先来 重现 一下这个问题,maven install报错,说 找不到这个包,但是其实 我已经安装了. 我们 再来 看看 maven本地仓库里面有 什么,这 ...

  10. Apache中的gzip压缩作用及配置

    gzip会对文本资源进行压缩,一般能节省40%的大小,二进制内容不需要开启Gzip压缩,因为这些文件是已经压缩过的,如果再进行gzip压缩可能反而会增加其大小,并且空耗cpu资源啊. 静态资源一般都会 ...