Advertisement
giGii

код

Aug 20th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 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. class SearchServer {
  51. public:
  52. void SetStopWords(const string& text) {
  53. for (const string& word : SplitIntoWords(text)) {
  54. stop_words_.insert(word);
  55. }
  56. }
  57.  
  58. /* void SetMinusWords(const string& text) {
  59. for (const string& word : SplitIntoWords(text)) {
  60. if (word[0] == '-') {
  61. minus_words_.insert(word);
  62. }
  63. }
  64. } */
  65.  
  66. void AddDocument(int document_id, const string& document) {
  67. const vector<string> words = SplitIntoWordsNoStop(document);
  68. documents_.push_back({document_id, words});
  69. }
  70.  
  71. vector<Document> FindTopDocuments(const string& raw_query) const {
  72. const PlusMinusWords query_words = ParseQuery(raw_query);
  73.  
  74. auto matched_documents = FindAllDocuments(query_words);
  75.  
  76. sort(matched_documents.begin(), matched_documents.end(),
  77. [](const Document& lhs, const Document& rhs) {
  78. return lhs.relevance > rhs.relevance;
  79. });
  80. if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  81. matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  82. }
  83. return matched_documents;
  84. }
  85.  
  86. private:
  87. struct DocumentContent {
  88. int id = 0;
  89. vector<string> words;
  90. };
  91.  
  92. struct PlusMinusWords {
  93. set<string> plus_words;
  94. set<string> minus_words;
  95. };
  96.  
  97. vector<DocumentContent> documents_;
  98.  
  99. set<string> stop_words_;
  100.  
  101. bool IsStopWord(const string& word) const {
  102. return stop_words_.count(word) > 0;
  103. }
  104.  
  105. vector<string> SplitIntoWordsNoStop(const string& text) const {
  106. vector<string> words;
  107. for (const string& word : SplitIntoWords(text)) {
  108. if (!IsStopWord(word)) {
  109. words.push_back(word);
  110. }
  111. }
  112. return words;
  113. }
  114.  
  115. PlusMinusWords ParseQuery(const string& text) const {
  116. PlusMinusWords query_words;
  117. for (const string& word : SplitIntoWordsNoStop(text)) {
  118. if (word[0] == '-') {
  119. query_words.minus_words.insert(word.substr(1, word.size()));
  120. } else {
  121. if (query_words.minus_words.count(word) == 0) {
  122. query_words.plus_words.insert(word);
  123. }
  124. }
  125. }
  126. return query_words;
  127. }
  128.  
  129. vector<Document> FindAllDocuments(const PlusMinusWords& query_words) const {
  130. vector<Document> matched_documents;
  131. for (const auto& document : documents_) {
  132. const int relevance = MatchDocument(document, query_words);
  133. if (relevance > 0) {
  134. matched_documents.push_back({document.id, relevance});
  135. }
  136. }
  137. return matched_documents;
  138. }
  139.  
  140. static int MatchDocument(const DocumentContent& content, const PlusMinusWords& query_words) {
  141. set<string> matched_words;
  142. for (const string& word : content.words) {
  143. if (matched_words.count(word) != 0 || query_words.minus_words.count(word) != 0) {
  144. continue;
  145. }
  146. if (query_words.plus_words.count(word) != 0) {
  147. matched_words.insert(word);
  148. }
  149. }
  150. return static_cast<int>(matched_words.size());
  151. }
  152. };
  153.  
  154. SearchServer CreateSearchServer() {
  155. SearchServer search_server;
  156. search_server.SetStopWords(ReadLine());
  157.  
  158. const int document_count = ReadLineWithNumber();
  159. for (int document_id = 0; document_id < document_count; ++document_id) {
  160. search_server.AddDocument(document_id, ReadLine());
  161. }
  162.  
  163. return search_server;
  164. }
  165.  
  166. int main() {
  167. const SearchServer search_server = CreateSearchServer();
  168.  
  169. const string query = ReadLine();
  170.  
  171. for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  172. cout << "{ document_id = "s << document_id << ", "
  173. << "relevance = "s << relevance << " }"s << endl;
  174. }
  175.  
  176. cout << "это кириллица" << endl;
  177. }
  178. /*
  179. и в на с
  180. 3
  181. найден белый кот . на нём модный ошейник
  182. пушистый кот ищет хозяина . особые приметы : пушистый хвост
  183. в парке горького найден ухоженный пёс с выразительными глазами
  184. пушистый ухоженный кот -ошейник
  185.  
  186. и в на с
  187. 1
  188. в парке горького найден ухоженный пёс с выразительными глазами
  189. пушистый ухоженный кот -ошейник
  190.  
  191. and in on with
  192. 3
  193. white cat found . on him fancy collar
  194. fluffy cat looking master . special signs : fluffy tail
  195. in Gorky Park found well-groomed dog with expressive eyes
  196. fluffy well-groomed cat -collar
  197.  
  198. and in on with
  199. 1
  200. in Gorky Park found well-groomed dog with expressive eyes
  201. fluffy well-groomed cat -collar
  202. */
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement