Advertisement
chevengur

Вводный курс: основы C++ | Урок 3: Плюсы множеств

Aug 31st, 2023
120
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 <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. int CalculateSimilarity(vector<string> first, vector<string> second) {
  10.     vector<string>inter;
  11.     sort(first.begin(), first.end());
  12.     sort(second.begin(), second.end());
  13.     set_intersection(first.begin(), first.end(), second.begin(), second.end(), back_inserter(inter));
  14.     return inter.size();
  15. }
  16.  
  17. // SplitIntoWords разбивает строку text на слова и возвращает их в виде вектора
  18. // Слово - последовательность непробельных символов,
  19. // разделённых одним или более пробелов.
  20. vector<string> SplitIntoWords(string text) {
  21.     vector<string> words;
  22.     string word;
  23.     for (const char c : text) {
  24.         if (c == ' ') {
  25.             if (!word.empty()) {
  26.                 words.push_back(word);
  27.                 word.clear();
  28.             }
  29.         }
  30.         else {
  31.             word += c;
  32.         }
  33.     }
  34.     if (!word.empty()) {
  35.         words.push_back(word);
  36.     }
  37.     return words;
  38. }
  39.  
  40. int main() {
  41.     string query, description;
  42.  
  43.     getline(cin, query);
  44.     getline(cin, description);
  45.  
  46.     std::cout << CalculateSimilarity(SplitIntoWords(query), SplitIntoWords(description)) << endl;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement