本文主要是介绍1597 - Searching the Web (UVA),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接如下:
Online Judge
我的代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
// #define debugstd::vector<std::vector<std::string>> docs;
int N, M, line;
std::string str, word, s, t;
std::map<std::string, std::vector<std::set<int>>> mp;int main(){#ifdef debugfreopen("0.txt", "r", stdin);freopen("1.txt", "w", stdout);#endifscanf("%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 debugfclose(stdin);fclose(stdout);#endifreturn 0;
}
这篇关于1597 - Searching the Web (UVA)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!