230 - Borrowers (UVA)

题目链接如下:

Online Judge

代码如下:

#include 
#include 
#include 
#include 
#include 
// #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 vec, toReturn;
std::string str;
int loc;
std::map 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;
}

你可能感兴趣的:(UVA,c++)