Borrowers UVA - 230

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000+5;

struct book
{
string name,author;
bool operator < (const book & b)
{
if (author!=b.author) return author<b.author;
else return name<b.name;
}
} books[maxn];


map<string,int>state;
//0 borrow 1 on shelve 2 off shelve

int find_pre(int cur)
{
for(int i=cur-1; i>=0; i--)
{
if(state[books[i].name]==1) return i;
}
return -1;
}
int main()
{
//freopen("datain.txt","r",stdin);
//ios::sync_with_stdio(false);
string buf;
state.clear();
int n=0;
while(getline(cin,buf))
{
if(buf[0]=='E')break;
int pos=buf.find('"',1);
string name=buf.substr(0,pos+1); //cout<<name<<endl;
string author=buf.substr(pos+5);// cout<<author<<endl;
books[n].author=author,books[n].name=name;
state[books[n].name]=1;
n++;
}
sort(books,books+n);
while(getline(cin,buf))
{
if(buf[0]=='E')break;
if(buf[0]=='B')
{
buf=buf.substr(7); //cout<<buf<<endl;
state[buf]=0;
}
if(buf[0]=='R')
{
buf=buf.substr(7);//cout<<buf<<endl;
state[buf]=2;
}
if(buf[0]=='S')
{
for(int i=0; i<n; i++)
{
if(state[books[i].name]==2) //cout<<1;
{
int pos=find_pre(i);
if(pos==-1) cout<<"Put "<<books[i].name<<" first\n";
else cout<<"Put "<<books[i].name<<" after "<<books[pos].name<<endl;
state[books[i].name]=1;
}
}
puts("END");
}
}
}

Guess you like

Origin www.cnblogs.com/hanker99/p/11204843.html