UVA230 Posted on 2017-05-08 | In Competitive Programming , UVa BorrowersAbridged problem statement給定書名與作者 與 借還書歷史。在看到 SHELVE 時印出需要被上架的書籍的一些資訊。 Solution sketch就模擬吧! 很煩的那種… AC code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116#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;}