UVA 230

题意:图书馆有一些书。可以执行借书、还书操作,还有一个操作是输出当前还回来的书应该放到架子上的哪个位置。具体细节可以去看原题。

思路:将每本书都用map对应一个编号。用一个vector来记录哪些书是还回来的。用一个set来记录当前书架上还剩下哪些书。

借书操作:set.earse(x)。

还书操作:vector.push_back(x)

输出:将vector先排个序,然后一本一本的加回set里,再用set.lower_bound查找这本书的位置,根据情况输出即可。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
const int maxn = 10009;
vector< pair<string,string> > book;
map<string,int> pos;
string a[maxn];
int num;
char temp[300];

set<int> now;
vector<int> Back;

int main()
{
    num = 0;
    book.clear();
    string author,title;
    while(1)
    {
        gets(temp);
        if ( temp[0] == 'E' && temp[1] == 'N' && temp[2] == 'D' ) break;
        num++;
        int len = strlen(temp);
        int k = strchr(temp+1,'"')-temp;
        title = string(&temp[1],&temp[k]);
        author = string(&temp[k+5],&temp[len]);
        book.push_back( make_pair(author,title) );
    }
    sort(book.begin(),book.end());
    rep(i,0,num-1)
    {
        a[i] = book[i].second;
        pos[ a[i] ]= i;
    }

    now.clear();
    rep(i,0,num-1) now.insert(i);
    Back.clear();
    while(1)
    {
        gets(temp);
        if ( temp[0] == 'E' ) break;
        else if ( temp[0] == 'S' )
        {
            set<int>::iterator k;
            sort(Back.begin(),Back.end());
            rep(i,0,(int)Back.size()-1 )
            {
                now.insert( Back[i] );
                k = now.lower_bound( Back[i] );
                if ( k == now.begin() ) cout<<"Put \""<<a[Back[i]]<<"\" first"<<endl;
                else
                {
                    k--;
                    cout<<"Put \""<<a[Back[i]]<<"\" after \""<< a[ *k ] <<"\""<<endl;
                }
            }
            Back.clear();
            puts("END");
        }
        else
        {
            int k = strchr(temp,'"')-temp;
            title = string(&temp[k+1],&temp[strlen(temp)-1]);
            k = pos[title];
            if ( temp[0] == 'B' ) now.erase(k);
            else Back.push_back(k);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值