RobertDeMilo

Сортировка вектора структур

Oct 21st, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 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. set<string> ParseStopWords(const string& text) {
  46.     set<string> stop_words;
  47.     for (const string& word : SplitIntoWords(text)) {
  48.         stop_words.insert(word);
  49.     }
  50.     return stop_words;
  51. }
  52.  
  53. struct DocumentContent {
  54.     int id = 0;
  55.     vector<string> words;
  56. };
  57.  
  58. struct Document {
  59.     int id;
  60.     int relevance;
  61. };
  62.  
  63. bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  64.     return lhs.relevance > rhs.relevance;
  65. }
  66.  
  67. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  68.     vector<string> words;
  69.     for (const string& word : SplitIntoWords(text)) {
  70.         if (stop_words.count(word) == 0) {
  71.             words.push_back(word);
  72.         }
  73.     }
  74.     return words;
  75. }
  76.  
  77. void AddDocument(vector<DocumentContent>& documents, const set<string>& stop_words, int document_id,
  78.                  const string& document) {
  79.     const vector<string> words = SplitIntoWordsNoStop(document, stop_words);
  80.     documents.push_back({document_id, words});
  81. }
  82.  
  83. set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  84.     set<string> query_words;
  85.     for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  86.         query_words.insert(word);
  87.     }
  88.     return query_words;
  89. }
  90.  
  91. int MatchDocument(const DocumentContent& content, const set<string>& query_words) {
  92.     if (query_words.empty()) {
  93.         return 0;
  94.     }
  95.     set<string> matched_words;
  96.     for (const string& word : content.words) {
  97.         if (matched_words.count(word) != 0) {
  98.             continue;
  99.         }
  100.         if (query_words.count(word) != 0) {
  101.             matched_words.insert(word);
  102.         }
  103.     }
  104.     return static_cast<int>(matched_words.size());
  105. }
  106.  
  107. vector<Document> FindAllDocuments(const vector<DocumentContent>& documents,
  108.                                   const set<string>& query_words) {
  109.     vector<Document> matched_documents;
  110.     for (const auto& document : documents) {
  111.         const int relevance = MatchDocument(document, query_words);
  112.         if (relevance > 0) {
  113.             matched_documents.push_back({document.id, relevance});
  114.         }
  115.     }
  116.     return matched_documents;
  117. }
  118.  
  119. vector<Document> FindTopDocuments(const vector<DocumentContent>& documents,
  120.                                   const set<string>& stop_words, const string& raw_query) {
  121.     const set<string> query_words = ParseQuery(raw_query, stop_words);
  122.     auto matched_documents = FindAllDocuments(documents, query_words);
  123.  
  124.     sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  125.     if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  126.         matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  127.     }
  128.     return matched_documents;
  129. }
  130.  
  131. int main() {
  132.     const string stop_words_joined = ReadLine();
  133.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  134.  
  135.     // Read documents
  136.     vector<DocumentContent> documents;
  137.     const int document_count = ReadLineWithNumber();
  138.     for (int document_id = 0; document_id < document_count; ++document_id) {
  139.         AddDocument(documents, stop_words, document_id, ReadLine());
  140.     }
  141.  
  142.     const string query = ReadLine();
  143.     for (auto [document_id, relevance] : FindTopDocuments(documents, stop_words, query)) {
  144.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  145.              << endl;
  146.     }
  147. }
Add Comment
Please, Sign In to add comment