也是纯模拟题, 远没有上次高尔夫球赛问题复杂..
就构造一个Book结构, 把涉及的变量都加进去, 除了title, author还有一个state
注意有三种state, (一开始以为borrow指令是无意义的, 其实还是有影响的, 借出去的书遍历的时候需要直接跳过了)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <deque>
#include <map>
#include <cassert>
#include <iomanip>
#include <list>
using namespace std;
const int MAXN = 170;
typedef long long LL;
/*
uva 230
*/
const int notBorrowed = 0;
const int isBorrowed = 1;
const int isReturned = 2;
struct Book{
string title;
string author;
int state;
Book(){ state = notBorrowed; }
Book(string _title, string _author){
title = _title;
author = _author;
state = notBorrowed;
}
};
bool operator<(const Book & b1,const Book & b2){
if( b1.author!=b2.author ){
return b1.author < b2.author;
}
return b1.title < b2.title;
}
vector<Book> shelve;
string str;
char input[MAXN];
Book & Find(const string & title){
int i;
for(i=0; i<shelve.size(); i++){
if( shelve[i].title==title ){
break;
}
}
return shelve[i];
}
int main(){
// freopen("input2.txt","r",stdin);
string title, cmd,author;
shelve.clear();
while( cin>>input[0] && input[0]!='E' ){
fgets(input+1,MAXN,stdin);
input[strlen(input)-1] = '\0';
char * ptr = strstr(input," by ");
ptr[0] = '\0';
title = input;
author = (ptr+4);
shelve.push_back(Book(title,author));
}
while( cin>>cmd && cmd!="END" ){
if( cmd=="BORROW" ){
getchar();
getline(cin,title);
Find(title).state = isBorrowed;
}else if( cmd=="RETURN" ){
getchar();
getline(cin,title);
Find(title).state = isReturned;
}else if( cmd=="SHELVE" ){
int lastBook = -1;
sort(shelve.begin(),shelve.end(),less<Book>());
for(int i=0; i<shelve.size(); i++){
// cout << shelve[i].title << "---" << shelve[i].author << " : " << shelve[i].state << endl;
if( shelve[i].state==notBorrowed ){
lastBook = i;
}else if( shelve[i].state==isBorrowed ){
continue;
}else if( shelve[i].state==isReturned ){
shelve[i].state = notBorrowed;
if( lastBook<0 ){
// put first
cout << "Put " << shelve[i].title << " first" << endl;
}else{
// put after a book
cout << "Put " << shelve[i].title << " after " << shelve[lastBook].title << endl;
}
lastBook = i;
}
}
cout << "END" << endl;
}
}
return 0;
}