Advertisement
chevengur

Вводный курс: основы C++ | Урок 3: Ссылки 3/3

Sep 1st, 2023
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> SplitIntoWords(string& text) {
  9.     vector<string> words;
  10.     string word;
  11.     for (char c : text) {
  12.         if (c == ' ') {
  13.             if (!word.empty()) {
  14.                 words.push_back(word);
  15.                 word.clear();
  16.             }
  17.         } else {
  18.             word += c;
  19.         }
  20.     }
  21.     if (!word.empty()) {
  22.         words.push_back(word);
  23.     }
  24.     return words;
  25. }
  26.  
  27. set<string> ParseStopWords(string& text) {
  28.     set<string> stop_words;
  29.     for (string& word : SplitIntoWords(text)) {
  30.         stop_words.insert(word);
  31.     }
  32.     return stop_words;
  33. }
  34.  
  35. vector<string> ParseQuery(string& text, set<string>& stop_words) {
  36.     vector<string> words;
  37.     for (string& word : SplitIntoWords(text)) {
  38.         if (stop_words.count(word) == 0) {
  39.             words.push_back(word);
  40.         }
  41.     }
  42.     return words;
  43. }
  44.  
  45. int main() {
  46.     // Read stop words
  47.     string stop_words_joined;
  48.     getline(cin, stop_words_joined);
  49.     set<string> stop_words = ParseStopWords(stop_words_joined);
  50.    
  51.     // Read query
  52.     string query;
  53.     getline(cin, query);
  54.     vector<string> query_words = ParseQuery(query, stop_words);
  55.    
  56.     for (string word : query_words) {
  57.         cout << '[' << word << ']' << endl;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement