UVA borrowers (pick) <vector> "structure" worship!

Source: Internet
Author: User

I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves, and creators O F odd volumes. --charles Lamb, Essays of Elia (1823) ' The Races of Men '

Like Mr Lamb, librarians has their problems with borrowers too. People don ' t put books back where they should. Instead, returned books was kept at the main desk until a librarian was free to replace them in the right places on the She Lves. Even for librarians, putting "right" in the "right place" can be very time-consuming. But since many libraries is now computerized, 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 would ask your program for a list of books that has been returned so the books can be Returne D to their correct places on the shelves. Before they is returned to the shelves, the returned books is 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. Your program should tell the librarian which book (including those previously shelved) are already on the SH Elf before which the returned book should go. 

  Input First, the stock of the library is listed, one book per line, in no particular order. Initially, they is all on the shelves. No. Books has the same title. The format of each line would be:  "title" by author  the end of the stock listing would be marked by a line Conta Ining only the word:  end  following the stock list would be a series of records of books borrowed and returned, and requests from librarians to assistance in restocking the shelves. Each record would appear on a single line, in one of the following formats:  borrow "title" RETURN "title" shelve&nbsp ; The list would be terminated to a line containing only the word:  end        & nbsp;          Output each time the SHELVE command appears, your program Should output a series of instructions for the librarian, one per line, with the format:  Put "Title1" after "Title2" &N Bsp Or, for the specIal case of the book being the first on the collection:  Put "title" first  after the set of instructions for EA Ch SHELVE, output a line containing only the word:  END assumptions & limitations  a title are at most Cha Racters long.  An author are at most characters long.  A title would 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.endborrow" algorithms "borrow" the C programming Language "retur N "Algorithms" RETURN "the C programming Language" Shelveend
Sample Output
Put "The C programming Language" after "The Canterbury Tales" put "algorithms" after "the C programming Language" END


It's a really hard thing to see the input and output of all kinds of troublesome strings.
The problem with the feeling of data processing is indeed more troublesome
The following is the code that is picked up
#include <algorithm>#include<cstdio>#include<iostream>#include<string>#include<string.h>#include<vector>using namespacestd;structbook{stringtitle, author; BOOLborrowed, returned; Book (stringAstringb) {Title=A; Author=b; Borrowed= returned =false; }};vector<book>Allbook;stringCommand,request,ss;BOOLCmpa (book A,book b) {return(A.author <b.author);}BOOLcmpt (book A,book b) {return(A.title <b.title);}voidshelve () {intJ;  for(inti =0; I < allbook.size (); i++){        if(allbook[i].returned = =true){             for(j = i; J >=0; j--){                if(allbook[j].borrowed = =false) Break; }            if(j = =-1) printf ("Put%s first\n", Allbook[i].title.c_str ());//all the books that were before were borrowed.            Elseprintf"Put%s after%s\n", Allbook[i].title.c_str (), Allbook[j].title.c_str ()); Allbook[i].borrowed= allbook[i].returned =false; }} cout<<"end\n";}voidBorrow () {getline (CIN, request);  for(inti =0; i < allbook.size (); i++){        if(Allbook[i].title = =request) {allbook[i].borrowed=true; return; }    }}voidBack () {getline (CIN, request);  for(inti =0; I < allbook.size (); i++ ) {        if(Allbook[i].title = =request) {allbook[i].returned=true; return; }    }}intMain () { while(Getline (CIN,SS) &&ss!="END") {allbook.push_back (Ss.substr (0, Ss.find_last_of ("\"") +1), Ss.substr (Ss.find_last_of ("\"") +1 )));    } stable_sort (Allbook.begin (), Allbook.end (), cmpt);    Stable_sort (Allbook.begin (), Allbook.end (), CMPA);  while(Cin >>command) {         if(Command = ="Borrow") cin.Get(), Borrow (); Else if(Command = ="RETURN") cin.Get(), Back (); Else if(Command = ="SHELVE") cin.Get(), shelve (); }    //System ("pause");    return 0;}

Convert a string in C + + to the%s output in C, using C_STR (), otherwise the output is a bunch of garbled
After reading the operation is not very difficult, but the idea and the processing of strings are worthy of worship!



UVA borrowers (pick) <vector> "structure" worship!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.