Praktikum_mf

Лямбда-функции/Компаратор для сортировки/Задание 1

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