uva230-Borrowers

来源:互联网 发布:淘宝固定悬浮导航 编辑:程序博客网 时间:2024/05/16 11:35

题意:1、输入若干图书标题和作者。

              2、若干指令: BORROW表示借书, RETURN表示还书,SHELVE表示把所有已经归还但还未上架的图书排序后一次插入书架并输出图书标题和插入位置。

              注:图书排序是先按作者从小到大排,再按标题从小到大排。


思路:定义结构体关联标题和作者,用map存标题并编号,借出去的书改编号为0,还回来但还未上架的数改编号为-1,每次发出SHELVE指令时,在map里面扫一遍就行。


代码:

#include <cstdio>#include <cstring>#include <sstream>#include <iostream>#include <map>#include <algorithm>#include <cstring>#include <iomanip>#include <string>using namespace std;map<string ,int> mp;struct book{    string title;    string author;    bool operator < (const struct book& ths) {        return author < ths.author || (author == ths.author && title < ths.title);     }}bk[1000+10];int main(){   int i=0;   string line, x, y;   mp.clear();   while (getline(cin, line)) {    if (line == "END") break;    x = line.substr(0, line.find_last_of("\"")+1);    y = line.substr(line.find_last_of("\"")+5);    bk[i].title = x;    bk[i++].author = y;   }   sort(bk, bk+i);   for (int j=0; j<i; j++) {    mp[bk[j].title] = j+1;   }   while (cin >> x) {    if (x == "END") break;    if (x == "BORROW") {        getchar();        getline(cin, y); mp[y] = 0;    }    if (x == "RETURN") {        getchar();        getline(cin, y);  mp[y] = -1;    }    if (x == "SHELVE") {        for (int j=0; j<i; j++) {           if (mp[bk[j].title] == -1) {           if (j == 0) {            printf("Put %s first\n", bk[j].title.c_str());           }           else {               int k = j-1;               while (mp[bk[k].title] == 0) {                 k--; if (k < 0) break;                }               if (k == -1)  printf("Put %s first\n", bk[j].title.c_str());               else  printf("Put %s after %s\n", bk[j].title.c_str(),  bk[k].title.c_str());           }            mp[bk[j].title]  = j+1;           }        }      puts("END");    }   }  return 0;}



0 0
原创粉丝点击