• UVa230 Borrowers (STL)


     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:

    ``titleby 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 ``titlefirst

    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
    这是一个STL的题目,好久没写STL了,就写了一个这个题目,尽量用STL写的。。
    注意空格和双引号;
      1 #include<iostream>
      2 #include<algorithm>
      3 #include<string>
      4 #include<sstream>
      5 #include<set>
      6 #include<map>
      7 using namespace std;
      8 struct node
      9 {
     10     string name,author; //书名和作者
     11     friend bool operator < (node a,node b)
     12     {
     13         if(a.author==b.author)  return a.name<b.name;
     14         return a.author<b.author;
     15     }
     16 };
     17 set<node> q; //这个是图书馆的书架上面的书
     18 set<node> p; //这个是图书馆准备被放上书架的书,就是别人刚还的书,还没来得及放上书架
     19 map<string,string> Mp; //这是一个书名指向作者的映射
     20 void deal(string s) // 把输入的作者和书名放入结构体
     21 {
     22     node e;
     23     int len=s.size();
     24     for(int i=0;i<len;i++) if(s[i]=='"') s[i]=' ';
     25     stringstream ss(s);
     26     string temp;
     27     int flag=0,flagch=0;
     28     while(ss >> temp)
     29     {
     30         if(temp=="by") {flag=0;flagch=1;continue;}
     31         if(flagch==0)
     32         {
     33             if(flag++) e.name+=" ";
     34             e.name+=temp;
     35         }
     36         else
     37         {
     38             if(flag++)  e.author+=" ";
     39             e.author+=temp;
     40         }
     41     }
     42     Mp[e.name]=e.author;
     43     q.insert(e);
     44     return;
     45 }
     46 string fun(string st) //fun() 函数用于去掉输入的引号
     47 {
     48     string rev;
     49     int len=st.size();
     50     for(int i=0;i<len;i++) if(st[i]=='"') st[i]=' ';
     51     int flag=0;
     52     stringstream ss(st);
     53     string temp;
     54     while(ss >> temp)
     55     {
     56         if(flag++) rev+=" ";
     57         rev+=temp;
     58     }
     59     return rev;
     60 }
     61 int main()
     62 {
     63     string temp;
     64     int num=0;
     65     while(getline(cin,temp)) //输入,处理,然后把书放上书架
     66     {
     67         if(temp=="END") break;
     68         deal(temp);
     69     }
     70     while(cin >> temp) //执行指令,
     71     {
     72         if(temp=="END") break;
     73         else if(temp=="SHELVE") //把书放上书架
     74         {
     75             while(!p.empty())
     76             {
     77                 node e=*p.begin();
     78                 p.erase(e);
     79                 set<node>::iterator it=q.lower_bound(e);
     80                 if(it==q.begin())  cout << "Put "" << e.name << "" first" << endl;
     81                 else
     82                 {
     83                     it--;
     84                     cout << "Put "" << e.name << "" after "" << (*it).name << """ << endl;
     85                 }
     86                 q.insert(e);
     87             }
     88             cout << "END" << endl;
     89         }
     90         else //可以一起处理
     91         {
     92             string book;
     93             getline(cin,book);
     94             book=fun(book); 
     95             string author=Mp[book];
     96             node e;
     97             e.name=book;
     98             e.author=author;
     99             if(temp=="BORROW")   q.erase(e);
    100             if(temp=="RETURN")   p.insert(e);
    101         }
    102     }
    103     return 0;
    104 }
    View Code
  • 相关阅读:
    字符串内部查找函数
    vs2005 编译zlib 1.2.3 小记
    ies4linux 安装
    详述IP数据包的转发流程
    看源代码
    091213
    值得你记住并受用一生的Word XP/2003快捷键
    java开源框架的源代码怎么读?
    excel中的EMBED域介绍
    如何用c语言实现CString的构造函数、析构函数和赋值函数?
  • 原文地址:https://www.cnblogs.com/I-love-HLD/p/4368883.html
Copyright © 2020-2023  润新知