RobertDeMilo

Контейнер pair

Oct 21st, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. string ReadLine() {
  9.     string s;
  10.     getline(cin, s);
  11.     return s;
  12. }
  13.  
  14. int ReadLineWithNumber() {
  15.     int result = 0;
  16.     cin >> result;
  17.     ReadLine();
  18.     return result;
  19. }
  20.  
  21. vector<string> SplitIntoWords(const string& text) {
  22.     vector<string> words;
  23.     string word;
  24.     for (const char c : text) {
  25.         if (c == ' ') {
  26.             if (!word.empty()) {
  27.                 words.push_back(word);
  28.                 word.clear();
  29.             }
  30.         } else {
  31.             word += c;
  32.         }
  33.     }
  34.     if (!word.empty()) {
  35.         words.push_back(word);
  36.     }
  37.  
  38.     return words;
  39. }
  40.  
  41. set<string> ParseStopWords(const string& text) {
  42.     set<string> stop_words;
  43.     for (const string& word : SplitIntoWords(text)) {
  44.         stop_words.insert(word);
  45.     }
  46.     return stop_words;
  47. }
  48.  
  49. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  50.     vector<string> words;
  51.     for (const string& word : SplitIntoWords(text)) {
  52.         if (stop_words.count(word) == 0) {
  53.             words.push_back(word);
  54.         }
  55.     }
  56.     return words;
  57. }
  58.  
  59. void AddDocument(vector<pair<int, vector<string>>>& documents, const set<string>& stop_words,
  60.                  int document_id, const string& document) {
  61.     const vector<string> words = SplitIntoWordsNoStop(document, stop_words);
  62.     documents.push_back(pair<int, vector<string>>{document_id, words});
  63. }
  64.  
  65. set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  66.     set<string> query_words;
  67.     for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  68.         query_words.insert(word);
  69.     }
  70.     return query_words;
  71. }
  72.  
  73. int MatchDocument(const pair<int, vector<string>>& content, const set<string>& query_words) {
  74.     if (query_words.empty()) {
  75.         return 0;
  76.     }
  77.     set<string> matched_words;
  78.     for (const string& word : content.second) {
  79.         if (matched_words.count(word) != 0) {
  80.             continue;
  81.         }
  82.         if (query_words.count(word) != 0) {
  83.             matched_words.insert(word);
  84.         }
  85.     }
  86.     return static_cast<int>(matched_words.size());
  87. }
  88.  
  89. // Для каждого документа возвращает его id и релевантность
  90. vector<pair<int, int>> FindDocuments(const vector<pair<int, vector<string>>>& documents,
  91.                                      const set<string>& stop_words,
  92.                                      const string& query) {
  93.     const set<string> query_words = ParseQuery(query, stop_words);
  94.     vector<pair<int, int>> matched_documents;
  95.     for (const auto& document : documents) {
  96.         const int relevance = MatchDocument(document, query_words);
  97.         if (relevance > 0) {
  98.             matched_documents.push_back({document.first, relevance});
  99.         }
  100.     }
  101.     return matched_documents;
  102. }
  103.  
  104. int main() {
  105.     const string stop_words_joined = ReadLine();
  106.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  107.  
  108.     // Read documents
  109.     vector<pair<int, vector<string>>> documents;
  110.     const int document_count = ReadLineWithNumber();
  111.     for (int document_id = 0; document_id < document_count; ++document_id) {
  112.         AddDocument(documents, stop_words, document_id, ReadLine());
  113.     }
  114.  
  115.     const string query = ReadLine();
  116.     for (auto [document_id, relevance] : FindDocuments(documents, stop_words, query)) {
  117.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  118.              << endl;
  119.     }
  120. }
Add Comment
Please, Sign In to add comment