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;
}
相关推荐
蜡笔小马1 小时前
03.C++设计模式-原型模式
c++·设计模式·原型模式
神仙别闹1 小时前
基于QT(C++)实现线性表的建立、插入、删除、查找等基本操作
java·c++·qt
salipopl2 小时前
C/C++ 中 volatile 关键字详解:原理、作用与实际应用
开发语言·c++
张赫轩(不重名)2 小时前
图论3:连通性问题(复杂度均为 O(N + M) )
c++·算法·图论·拓扑学
AIminminHu2 小时前
(让 C++ 程序长出大脑:从“语音遥控器”到具身智能 Agent 的进化之路)------OpenGL渲染与几何内核那点事------(二-1-(15))
开发语言·c++·agent·具身智能
君义_noip2 小时前
CSP-J 2025 入门级 第一轮(初赛) 完善程序(1)
c++·算法·信息学奥赛·csp 第一轮
哭泣方源炼蛊3 小时前
AtCoder Beginner Contest 456 E补题(分层图 + 有向环检测 )
c++·算法·深度优先·图论·拓扑学
Yuk丶4 小时前
UE4 与 UE5:技术差异深度解析
c++·ue5·游戏引擎·ue4·游戏程序·虚幻
故事和你914 小时前
洛谷-数据结构2-1-二叉堆与树状数组1
开发语言·数据结构·c++·算法·动态规划·图论
海参崴-4 小时前
C++ STL篇 红黑树的模拟实现
开发语言·c++