Advertisement
chevengur

СПРИНТ № 1 | Лямбда-функции | Урок 7: Ускоряем поиск документов

Sep 28th, 2023 (edited)
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7. #include <map>
  8.  
  9. using namespace std;
  10.  
  11. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  12.  
  13. string ReadLine() {
  14.     string s;
  15.     getline(cin, s);
  16.     return s;
  17. }
  18.  
  19. int ReadLineWithNumber() {
  20.     int result = 0;
  21.     cin >> result;
  22.     ReadLine();
  23.     return result;
  24. }
  25.  
  26. vector<string> SplitIntoWords(const string& text) {
  27.     vector<string> words;
  28.     string word;
  29.     for (const char c : text) {
  30.         if (c == ' ') {
  31.             if (!word.empty()) {
  32.                 words.push_back(word);
  33.                 word.clear();
  34.             }
  35.         }
  36.         else {
  37.             word += c;
  38.         }
  39.     }
  40.     if (!word.empty()) {
  41.         words.push_back(word);
  42.     }
  43.  
  44.     return words;
  45. }
  46.  
  47. struct Document {
  48.     int id;
  49.     int relevance;
  50. };
  51.  
  52. class SearchServer {
  53. public:
  54.     void SetStopWords(const string& text) {
  55.         for (const string& word : SplitIntoWords(text)) {
  56.             stop_words_.insert(word);
  57.         }
  58.     }
  59.  
  60.     void AddDocument(int document_id, const string& document) {
  61.         const auto words = ParseQuery(document);
  62.         for (const auto& word : words.plus_word) {
  63.             word_to_documents_[word].insert(document_id);
  64.         }
  65.     }
  66.  
  67.     vector<Document> FindTopDocuments(const string& raw_query) const {
  68.         auto query_words = ParseQuery(raw_query);
  69.         auto matched_documents = FindAllDocuments(query_words);
  70.  
  71.         sort(matched_documents.begin(), matched_documents.end(),
  72.             [](const Document& lhs, const Document& rhs) {
  73.                 return lhs.relevance > rhs.relevance;
  74.             });
  75.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  76.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  77.         }
  78.         return matched_documents;
  79.     }
  80.  
  81. private:
  82.  
  83.     struct Query {
  84.         set<string>minus_word;
  85.         set<string>plus_word;
  86.     };
  87.  
  88.     map<string, set<int>> word_to_documents_;
  89.  
  90.     set<string> stop_words_;
  91.  
  92.     bool IsStopWord(const string& word) const {
  93.         return stop_words_.count(word) > 0;
  94.     }
  95.  
  96.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  97.         vector<string> words;
  98.         for (const string& word : SplitIntoWords(text)) {
  99.             if (!IsStopWord(word)) {
  100.                 words.push_back(word);
  101.             }
  102.         }
  103.         return words;
  104.     }
  105.  
  106.     Query ParseQuery(const string& text) const {
  107.         Query query_words;
  108.         for (const string& word : SplitIntoWordsNoStop(text)) {
  109.             if (word[0] == '-') {
  110.                 query_words.minus_word.insert(word.substr(1));
  111.             }
  112.             else {
  113.                 query_words.plus_word.insert(word);
  114.             }
  115.  
  116.         }
  117.         return query_words;
  118.     }
  119.  
  120.     vector<Document> FindAllDocuments(const Query& query_words) const {
  121.         vector<Document> matched_documents;
  122.         Query words;
  123.         map<int, int>document_to_relevance;
  124.         for (const auto& [document, count] : word_to_documents_) {
  125.             for (const auto& document_id : count) {
  126.                 if (words.plus_word.count(document)) {
  127.                     ++document_to_relevance[document_id];
  128.                 }
  129.                 if (words.minus_word.count(document)) {
  130.                     break;
  131.                 }
  132.             }
  133.         }
  134.         for (const auto& [document_id, relevance] : document_to_relevance) {
  135.             if (relevance > 0) {
  136.                 matched_documents.push_back({ document_id, relevance });
  137.             }
  138.         }
  139.         return matched_documents;
  140.     }
  141. };
  142.  
  143. SearchServer CreateSearchServer() {
  144.     SearchServer search_server;
  145.     search_server.SetStopWords(ReadLine());
  146.  
  147.     const int document_count = ReadLineWithNumber();
  148.     for (int document_id = 0; document_id < document_count; ++document_id) {
  149.         search_server.AddDocument(document_id, ReadLine());
  150.     }
  151.  
  152.     return search_server;
  153. }
  154.  
  155. int main() {
  156.     const SearchServer search_server = CreateSearchServer();
  157.  
  158.     const string query = ReadLine();
  159.  
  160.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  161.         cout << "{ document_id = "s << document_id << ", "
  162.             << "relevance = "s << relevance << " }"s << endl;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement