shushus

Спринт 1. Методы классов. Задание 5.

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