Advertisement
chevengur

СПРИНТ № 1 | Структуры и классы | Урок 7: Константные методы

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