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;
}
相关推荐
mit6.8244 小时前
[openvela] Hello World :从零开始的完整实践与问题复盘
c++·嵌入式硬件
啊阿狸不会拉杆6 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路6 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
曙曙学编程7 小时前
stm32——GPIO
c语言·c++·stm32·单片机·嵌入式硬件
△曉風殘月〆7 小时前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试
武当豆豆8 小时前
C++编程学习(第25天)
开发语言·c++·学习
minji...11 小时前
C++ string类(STL简介 , string类 , 访问修改字符)
开发语言·c++
Forward♞11 小时前
Qt——文件操作
开发语言·c++·qt
十五年专注C++开发12 小时前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建
winds~13 小时前
【git】 撤销revert一次commit中的某几个文件
linux·c++