UVA230

Borrowers

Abridged problem statement

給定書名與作者 與 借還書歷史。在看到 SHELVE 時印出需要被上架的書籍的一些資訊。

Solution sketch

就模擬吧! 很煩的那種…

AC code

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
struct Entry {
string title;
string author;
bool inStock;
bool pending;
Entry(string title, string author, bool inStock)
{
this->title = title;
this->author = author;
this->inStock = inStock;
this->pending = false;
}
bool operator<(const Entry &other) const
{
if (author == other.author)
return title < other.title;
return author < other.author;
}
};
void clean(char *str)
{
int len = strlen(str);
while (str[len - 1] == '\n' || str[len - 1] == '\r') {
str[len - 1] = '\0';
len--;
}
}
int main()
{
char inp[10000];
vector<Entry> data;
while (fgets(inp, 10000, stdin) != NULL) {
if (strcmp(inp, "END\n") == 0)
break;
clean(inp);
int len = strlen(inp);
int quote = -1;
for (int i = 0; i < len; i++) {
if (inp[i] == '"') {
if (quote == -1)
quote = i;
else {
quote = i;
break;
}
}
}
char title[10000] = {'\0'}; // bitch, \0 is required
strncpy(title, inp + 1, quote - 1);
char author[10000] = {'\0'};
strncpy(author, inp + quote + 5, len - (quote + 5));
data.push_back(Entry(title, author, true));
}
sort(data.begin(), data.end());
while (fgets(inp, 10000, stdin) != NULL) {
if (inp[0] == 'E')
break;
clean(inp);
if (inp[0] == 'B') {
string inp1 = inp;
string title = inp1.substr(8, inp1.length() - 9);
for (int i = 0; i < (int)data.size(); i++)
if (data[i].title == title) {
data[i].inStock = false;
data[i].pending = false;
}
} else if (inp[0] == 'R') {
string inp1 = inp;
string title = inp1.substr(8, inp1.length() - 9);
for (int i = 0; i < (int)data.size(); i++)
if (data[i].title == title) {
data[i].pending = true;
}
} else {
// shelve
int prev = -1;
for (int i = 0; i < (int)data.size(); i++) {
if (data[i].inStock)
prev = i;
if (data[i].pending == true) {
if (prev == -1)
printf("Put \"%s\" first\n", data[i].title.c_str());
else
printf("Put \"%s\" after \"%s\"\n", data[i].title.c_str(),
data[prev].title.c_str());
data[i].pending = false;
data[i].inStock = true;
prev = i;
}
}
printf("END\n");
}
}
return 0;
}