题意:图书馆有一些书。可以执行借书、还书操作,还有一个操作是输出当前还回来的书应该放到架子上的哪个位置。具体细节可以去看原题。
思路:将每本书都用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;
}