Advertisement
RobertDeMilo

Константные методы

Oct 21st, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  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.         } else {
  35.             word += c;
  36.         }
  37.     }
  38.     if (!word.empty()) {
  39.         words.push_back(word);
  40.     }
  41.  
  42.     return words;
  43. }
  44.  
  45. struct Document {
  46.     int id;
  47.     int relevance;
  48. };
  49.  
  50. bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  51.     return lhs.relevance > rhs.relevance;
  52. }
  53.  
  54. class SearchServer {
  55. public:
  56.     void SetStopWords(const string& text) {
  57.         for (const string& word : SplitIntoWords(text)) {
  58.             stop_words_.insert(word);
  59.         }
  60.     }
  61.  
  62.     void AddDocument(int document_id, const string& document) {
  63.         const vector<string> words = SplitIntoWordsNoStop(document);
  64.         documents_.push_back({document_id, words});
  65.     }
  66.  
  67.     vector<Document> FindTopDocuments(const string& raw_query) const {
  68.         const set<string> query_words = ParseQuery(raw_query);
  69.         auto matched_documents = FindAllDocuments(query_words);
  70.  
  71.         sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  72.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  73.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  74.         }
  75.         return matched_documents;
  76.     }
  77.  
  78. private:
  79.     struct DocumentContent {
  80.         int id = 0;
  81.         vector<string> words;
  82.     };
  83.  
  84.     vector<DocumentContent> documents_;
  85.  
  86.     set<string> stop_words_;
  87.  
  88.     bool IsStopWord(const string& word) const {
  89.         return stop_words_.count(word) > 0;
  90.     }
  91.  
  92.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  93.         vector<string> words;
  94.         for (const string& word : SplitIntoWords(text)) {
  95.             if (!IsStopWord(word)) {
  96.                 words.push_back(word);
  97.             }
  98.         }
  99.         return words;
  100.     }
  101.  
  102.     set<string> ParseQuery(const string& text) const {
  103.         set<string> query_words;
  104.         for (const string& word : SplitIntoWordsNoStop(text)) {
  105.             query_words.insert(word);
  106.         }
  107.         return query_words;
  108.     }
  109.  
  110.     vector<Document> FindAllDocuments(const set<string>& query_words) const {
  111.         vector<Document> matched_documents;
  112.         for (const auto& document : documents_) {
  113.             const int relevance = MatchDocument(document, query_words);
  114.             if (relevance > 0) {
  115.                 matched_documents.push_back({document.id, relevance});
  116.             }
  117.         }
  118.         return matched_documents;
  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. SearchServer CreateSearchServer() {
  139.     SearchServer search_server;
  140.     search_server.SetStopWords(ReadLine());
  141.  
  142.     const int document_count = ReadLineWithNumber();
  143.     for (int document_id = 0; document_id < document_count; ++document_id) {
  144.         search_server.AddDocument(document_id, ReadLine());
  145.     }
  146.  
  147.     return search_server;
  148. }
  149.  
  150. int main() {
  151.     const SearchServer search_server = CreateSearchServer();
  152.  
  153.     const string query = ReadLine();
  154.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  155.         cout << "{ document_id = "s << document_id << ", "
  156.              << "relevance = "s << relevance << " }"s << endl;
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement