Advertisement
VinnRonn

server

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