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;
}
相关推荐
悄悄敲敲敲4 小时前
C++:dfs习题四则
c++·算法·深度优先
安於宿命5 小时前
【Linux】进程间通信——进程池
linux·c++
南郁10 小时前
001-监控你的文件-FSWatch-C++开源库108杰
c++·开源·文件系统·文件监控·fswatch·文件变动信息·libfswatch
linux开发之路11 小时前
C++Linux进阶项目分析-仿写Redis之Qedis
linux·c++·redis·多线程·后端开发
EPSDA11 小时前
Linux线程库与线程库封装
linux·运维·服务器·开发语言·c++
孤独得猿11 小时前
排序算法复习——包括插入排序、希尔排序、冒泡排序、快排(包括霍尔法、挖坑法、快慢指针法)、堆排、选择排序、归并排序等 (代码采用c/c++混编)
c语言·数据结构·c++·笔记·算法·排序算法
编程探索者小陈11 小时前
【C++】智能指针的使用及其原理
开发语言·c++
半桔13 小时前
贪吃蛇解析
c语言·开发语言·数据结构·c++·算法
Pan_peter13 小时前
零基础学QT、C++(一)安装QT
c++·qt
万能的小裴同学14 小时前
MFC 自定义十六进制显示控件
开发语言·c++·mfc