-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa-230.cpp
More file actions
100 lines (67 loc) · 2.32 KB
/
UVa-230.cpp
File metadata and controls
100 lines (67 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <cstdio>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h> // here we have all the STL we need, including istringstream and ostringstream
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
int main(void) {
FAST
vector<pair<string, string>> books_and_authors;
string s;
map<string, int> book_to_index;
while (getline(cin, s), s != "END") {
istringstream iss(s);
iss.ignore(1, '\"');
string book;
getline(iss, book, '\"');
string author;
// ignore " by "
iss.ignore(4, ' ');
getline(iss, author);
books_and_authors.emplace_back(author, book);
}
sort(ALL(books_and_authors));
for (int i = 0; i < books_and_authors.size(); i++) {
book_to_index[books_and_authors[i].second] = i;
}
vector<bool> isborrowed(books_and_authors.size(), false);
vector<bool> isreturned(books_and_authors.size(), false);
while (getline(cin, s), s != "END") {
istringstream iss(s);
string command;
iss >> command;
string book;
if (command != "SHELVE") {
iss.ignore(8, '\"');
getline(iss, book, '\"');
}
if (command == "BORROW") {
isborrowed[book_to_index[book]] = true;
isreturned[book_to_index[book]] = false;
} else if (command == "RETURN") {
isreturned[book_to_index[book]] = true;
} else if (command == "SHELVE") {
int last = -1;
for (int i = 0; i < books_and_authors.size(); i++) {
if (isreturned[i]) {
isborrowed[i] = false;
isreturned[i] = false;
if (last == -1) {
cout << "Put \"" << books_and_authors[i].second << "\" first\n";
} else {
cout << "Put \"" << books_and_authors[i].second << "\" after \"" << books_and_authors[last].second << "\"\n";
}
last = i;
}
else if (not isborrowed[i]) {
last = i;
}
}
cout << "END\n";
}
}
return 0;
}