1597 - Searching the Web (UVA)

题目链接如下:

Online Judge

我的代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
// #define debug

std::vector> docs;
int N, M, line;
std::string str, word, s, t;
std::map>> mp;

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d\n", &N);
    docs.resize(N);
    for (int i = 0; i < N; ++i){
        line = 0;
        while (getline(std::cin, str)){
            if (str == "**********"){
                break;
            }
            docs[i].push_back(str);
            for (int j = 0; j < str.size(); ++j){
                if (!isalpha(str[j])){
                    str[j] = ' ';
                } else {
                    str[j] = tolower(str[j]);
                }
            }
            std::istringstream in(str);
            while (in >> word){
                if (!mp.count(word)){
                    mp[word].resize(N);
                }
                mp[word][i].insert(line);
            }
            ++line;
        }
    }
    scanf("%d\n", &M);
    while (M--){
        getline(std::cin, str);
        bool flag = false;
        if (str.find("AND") != std::string::npos || str.find("OR") != std::string::npos){
            std::istringstream in(str);
            in >> s >> word >> t;
            if (!mp.count(s)){
                mp[s].resize(N);
            }
            if (!mp.count(t)){
                mp[t].resize(N);
            }
            for (int i = 0; i < N; ++i){
                if ((str.find("OR") != std::string::npos && (!mp[s][i].empty() || !mp[t][i].empty()))
                    || (str.find("AND") != std::string::npos && !mp[s][i].empty() && !mp[t][i].empty())){
                    printf("%s", flag ? "----------\n" : "");
                    flag = true;
                    for (int j = 0; j < docs[i].size(); ++j){
                        if (mp[s][i].count(j) || mp[t][i].count(j)){
                            printf("%s\n", docs[i][j].c_str());
                        }
                    }
                }
            }
        } else if (str.find("NOT") != std::string::npos){
            std::istringstream in(str);
            in >> word >> s;
            if (!mp.count(s)){
                mp[s].resize(N);
            }
            for (int i = 0; i < N; ++i){
                if (mp[s][i].empty()){
                    printf("%s", flag ? "----------\n" : "");
                    flag = true;
                    for (int j = 0; j < docs[i].size(); ++j){
                        printf("%s\n", docs[i][j].c_str());
                    }
                }
            }
        } else{
            if (mp.count(str)){
                for (int i = 0; i < N; ++i){
                    if (!mp[str][i].empty()){
                        printf("%s", flag ? "----------\n" : "");
                        flag = true;
                    }
                    for (int j = 0; j < docs[i].size(); ++j){
                        if (mp[str][i].count(j)){
                            printf("%s\n", docs[i][j].c_str());
                        }
                    }
                }
            }
        }
        printf("%s", flag ? "" : "Sorry, I found nothing.\n");
        printf("==========\n");
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

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