230 - Borrowers (UVA)

题目链接如下:

Online Judge

代码如下:

cpp 复制代码
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
// #define debug

struct book{
    std::string title;
    std::string author;
    bool isExisted = true;
    book(std::string _title, std::string _author): title(_title), author(_author){}
};
std::vector<book> vec, toReturn;
std::string str;
int loc;
std::map<std::string, int> mp;

bool cmp(const book &a, const book &b){
    return a.author != b.author ? a.author < b.author : a.title < b.title;
}

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    while (getline(std::cin, str) && str[0] != 'E'){
        int i;
        for (i = 1; i < str.size(); ++i){
            if (str[i] == '"'){
                break;
            }
        }
        vec.push_back(book(str.substr(0, i + 1), str.substr(i + 5)));
    }
    sort(vec.begin(), vec.end(), cmp);
    for (int i = 0; i < vec.size(); ++i){
        mp[vec[i].title] = i;
    }
    while (getline(std::cin, str) && str[0] != 'E'){
        if (str[0] == 'S'){
            sort(toReturn.begin(), toReturn.end(), cmp);
            for (int i = 0; i < toReturn.size(); ++i){
                loc = mp[toReturn[i].title];
                vec[loc].isExisted = true;
                int j;
                for (j = loc - 1; j >= 0; --j){
                    if (vec[j].isExisted){
                        break;
                    }
                }
                if (j < 0){
                    printf("Put %s first\n", toReturn[i].title.c_str());
                } else{
                    printf("Put %s after %s\n", toReturn[i].title.c_str(), vec[j].title.c_str());
                }
            }
            printf("END\n");
            toReturn.clear();
        } else{
            std::string temp = str.substr(7);
            if (str[0] == 'B'){
                vec[mp[temp]].isExisted = false;
            } else{
                toReturn.push_back(vec[mp[temp]]);
            }
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
相关推荐
YJlio7 分钟前
「C++ 40 周年」:从“野蛮生长的指针地狱”到 AI 时代的系统底座
c++·人工智能·oracle
纵有疾風起1 小时前
C++——多态
开发语言·c++·经验分享·面试·开源
冯诺依曼的锦鲤1 小时前
算法练习:差分
c++·学习·算法
Mr_WangAndy2 小时前
现代C++模板与泛型编程_第4章_remove_all_sequence,integer_sequence,is_union
c++·c++40周年·c++标准库用法
im_AMBER2 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
6***x5453 小时前
C++在计算机视觉中的图像处理
c++·图像处理·计算机视觉·游戏引擎·logback·milvus
fpcc3 小时前
跟我学C++中级篇——内存访问违例
c++
E***q5393 小时前
C++内存对齐优化
开发语言·c++
D_evil__3 小时前
[C++高频精进] 文件IO:文件操作
c++
别动哪条鱼4 小时前
MP4转AAC转换器C++
c++·ffmpeg·音视频·aac