Praktikum_mf

Структуры и классы/Константные методы/Задание 1

Sep 30th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  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;
  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.             words.push_back(word);
  32.             word = "";
  33.         } else {
  34.             word += c;
  35.         }
  36.     }
  37.     words.push_back(word);
  38.    
  39.     return words;
  40. }
  41.  
  42. struct Document {
  43.     int id;
  44.     int relevance;
  45. };
  46.  
  47. bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  48.     return lhs.relevance > rhs.relevance;
  49. }
  50.  
  51. class SearchServer {
  52. public:
  53.     void SetStopWords(const string& text) {
  54.         for (const string& word : SplitIntoWords(text)) {
  55.             stop_words_.insert(word);
  56.         }
  57.     }
  58.  
  59.     void AddDocument(int document_id, const string& document) {
  60.         for (const string& word : SplitIntoWordsNoStop(document)) {
  61.             word_to_documents_[word].insert(document_id);
  62.         }
  63.     }
  64.  
  65.     vector<Document> FindTopDocuments(const string& query) const {
  66.         auto matched_documents = FindAllDocuments(query);
  67.        
  68.         sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  69.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  70.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  71.         }
  72.         return matched_documents;
  73.     }
  74.  
  75. private:
  76.     map<string, set<int>> word_to_documents_;
  77.     set<string> stop_words_;
  78.  
  79.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  80.         vector<string> words;
  81.         for (const string& word : SplitIntoWords(text)) {
  82.             if (stop_words_.count(word) == 0) {
  83.                 words.push_back(word);
  84.             }
  85.         }
  86.         return words;
  87.     }
  88.  
  89.     vector<Document> FindAllDocuments(const string& query) const {
  90.         const vector<string> query_words = SplitIntoWordsNoStop(query);
  91.         map<int, int> document_to_relevance;
  92.         for (const string& word : query_words) {
  93.             if (word_to_documents_.count(word) == 0) {
  94.                 continue;
  95.             }
  96.             for (const int document_id : word_to_documents_.at(word)) {
  97.                 ++document_to_relevance[document_id];
  98.             }
  99.         }
  100.  
  101.         vector<Document> matched_documents;
  102.         for (auto [document_id, relevance] : document_to_relevance) {
  103.             matched_documents.push_back({document_id, relevance});
  104.         }
  105.    
  106.         return matched_documents;
  107.     }
  108. };
  109.  
  110. SearchServer CreateSearchServer() {
  111.     SearchServer search_server;
  112.     search_server.SetStopWords(ReadLine());
  113.  
  114.     const int document_count = ReadLineWithNumber();
  115.     for (int document_id = 0; document_id < document_count; ++document_id) {
  116.         search_server.AddDocument(document_id, ReadLine());
  117.     }
  118.    
  119.     return search_server;
  120. }
  121.  
  122.  
  123. int main() {
  124.     const SearchServer search_server = CreateSearchServer();
  125.  
  126.     const string query = ReadLine();
  127.     for (auto [document_id, relevance] : search_server.FindTopDocuments(query)) {
  128.         cout << "{ document_id = " << document_id << ", relevance = " << relevance << " }" << endl;
  129.     }
  130.  
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment