24h購物| | PChome| 登入
2013-07-18 10:26:19| 人氣2,492| 回應0 | 上一篇 | 下一篇

[UVA] 230 - Borrowers

推薦 0 收藏 0 轉貼0 訂閱站台


 Borrowers 

I mean your borrowers of books - those mutilators of collections, spoilers of the symmetry of shelves, and creators of odd volumes.

- (Charles Lamb, Essays of Elia (1823) `The Two Races of Men')

Like Mr. Lamb, librarians have their problems with borrowers too. People don't put books back where they should. Instead, returned books are kept at the main desk until a librarian is free to replace them in the right places on the shelves. Even for librarians, putting the right book in the right place can be very time-consuming. But since many libraries are now computerized, you can write a program to help.

When a borrower takes out or returns a book, the computer keeps a record of the title. Periodically, the librarians will ask your program for a list of books that have been returned so the books can be returned to their correct places on the shelves. Before they are returned to the shelves, the returned books are sorted by author and then title using the ASCII collating sequence. Your program should output the list of returned books in the same order as they should appear on the shelves. For each book, your program should tell the librarian which book (including those previously shelved) is already on the shelf before which the returned book should go.

Input

First, the stock of the library will be listed, one book per line, in no particular order. Initially, they are all on the shelves. No two books have the same title. The format of each line will be:

``title" by author

The end of the stock listing will be marked by a line containing only the word:

END

Following the stock list will be a series of records of books borrowed and returned, and requests from librarians for assistance in restocking the shelves. Each record will appear on a single line, in one of the following formats:

BORROW ``title"

RETURN ``title"

SHELVE

The list will be terminated by a line containing only the word:

END

Output

Each time the SHELVE command appears, your program should output a series of instructions for the librarian, one per line, in the format:

Put `` tex2html_wrap_inline61 " after `` tex2html_wrap_inline63 "

or, for the special case of the book being the first in the collection:

Put ``title" first

After the set of instructions for each SHELVE, output a line containing only the word:

END

Assumptions & Limitations:

1. A title is at most 80 characters long.

2. An author is at most 80 characters long.

3. A title will not contain the double quote (") character.

Sample Input

"The Canterbury Tales" by Chaucer, G.
"Algorithms" by Sedgewick, R.
"The C Programming Language" by Kernighan, B. and Ritchie, D.
END
BORROW "Algorithms"
BORROW "The C Programming Language"
RETURN "Algorithms"
RETURN "The C Programming Language"
SHELVE
END

Sample Output

Put "The C Programming Language" after "The Canterbury Tales"
Put "Algorithms" after "The C Programming Language"
END

會告訴你書架上的書目,但是排序要自己先做。

然後接下來會給定一些借出與歸還訊息。

當管理員把書放回時,會先將車上的書做一次排序,然後再依序放回書架上。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
struct E {
string title, author;
bool operator<(const E &a) const {
if(author != a.author)
return author < a.author;
return title < a.title;
}
};
E shelve[10005];
int main() {
char s[10005];
int n = 0;
int i, j, k;
while(gets(s)) {
if(!strcmp(s, "END"))
break;
for(i = 1; s[i] != '\"'; i++);
s[i] = '\0';
i += 2;
for(; s[i] != ' '; i++);
shelve[n].title = s+1;
shelve[n].author = s+i+1;
n++;
}
sort(shelve, shelve+n);
map<string, int> R;
int borrow[10005], ret[10005];
for(i = 0; i < n; i++) {
R[shelve[i].title] = i;
borrow[i] = 0, ret[i] = 0;
}
string cmd, book;
while(cin >> cmd) {
if(cmd == "SHELVE") {
int pre = -1;
for(i = 0; i < n; i++) {
if(ret[i] == 1) {
if(pre == -1) {
printf("Put \"%s\" first\n", shelve[i].title.c_str());
} else {
printf("Put \"%s\" after \"%s\"\n", shelve[i].title.c_str(), shelve[pre].title.c_str());
}
ret[i] = 0;
}
if(ret[i] == 0 && borrow[i] == 0)
pre = i;
}
puts("END");
} else if(cmd == "BORROW") {
getline(cin, book);
book = book.substr(2, book.length()-3);
borrow[R[book]] = 1;
ret[R[book]] = 0;
} else if(cmd == "RETURN"){
getline(cin, book);
book = book.substr(2, book.length()-3);
borrow[R[book]] = 0;
ret[R[book]] = 1;
} else
break;
}
return 0;
}

台長: Morris
人氣(2,492) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][生命遊戲] 12187 - Brothers
此分類上一篇:[UVA] 1209 - Wordfish

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文