UVa230 Borrowers

原题链接 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;
}

如有错误欢迎指出!!

转载于:https://www.cnblogs.com/flyawayl/p/8305611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值