昨天晚上看看了没思路就睡觉了,早上上近代史想了想,这个方法我感觉挺乱的,用了三个map,两个是提取书名用的,一个是判断有没有借出去,然后用优先队列给还的书排序。大体就这个么想法。
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<algorithm>
#include<cstring>
#define MAXN 200
using namespace std;
struct Title_author {
string title;
string author;
};
bool cmp_books (const Title_author T1, const Title_author T2) {
if(T1.author != T2.author)
return T1.author < T2.author;
return T1.title < T2.title;
}
priority_queue<int, vector<int>, greater<int> > Return;
vector<Title_author> Books;
map<string, int> books_list;
map<int, string> num_books;
map<string, bool> Is_exist;
Title_author put_books(char *s) {
int ant = 0;
Title_author temp_books;
while(*s != '\0') {
if(ant != 2)
temp_books.title += *s;
else
temp_books.author += *s;
if(*s == '"') {
ant++;
if(ant == 2) s += 4;
}
s++;
}
return temp_books;
}
string get_book(char *s) {
string b;
for(int i = 1; i < strlen(s); i++)
b += *(s + i);
return b;
}
void shelve_book() {
while(!Return.empty()) {
bool is_find = false;
int val = Return.top();
for(int i = val - 1; i >= 1; i--)
if(Is_exist[num_books[i]]) {
cout << "Put " << num_books[Return.top()] << " after "
<< num_books[i] << endl;
Is_exist[num_books[Return.top()]] = true;
Return.pop();
is_find = true;
break;
}
if(!is_find) {
cout << "Put " << num_books[Return.top()] << " first" << endl;
Is_exist[num_books[Return.top()]] = true;
Return.pop();
continue;
}
}
cout << "END" << endl;
}
int main() {
char cmd[10], title[MAXN];
while(gets(title)) {
if(title[0] == 'E') break;
Books.push_back(put_books(title));
}
sort(Books.begin(), Books.end(), cmp_books);
for(int i = 0; i < Books.size(); i++) {
books_list[Books[i].title] = i + 1;
Is_exist[Books[i].title] = true;
num_books[i + 1] = Books[i].title;
}
while(cin >> cmd) {
if(cmd[0] == 'E') break;
if(cmd[0] == 'S') {
shelve_book();
continue;
}
char temp[MAXN];
string book;
gets(temp);
book = get_book(temp);
if(cmd[0] == 'B') Is_exist[book] = false;
if(cmd[0] == 'R') Return.push(books_list[book]);
}
return 0;
}
浙公网安备 33010602011771号