mstoyanov7

letters.01

Jul 2nd, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <sstream>
  5. #include <cctype>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. std::set <std::string> getMessage() {
  10.     std::string input;
  11.     getline(std::cin, input);
  12.  
  13.     for (size_t i = 0; i < input.size(); ++i) {
  14.         if (ispunct(input[i])) {
  15.             input[i] = ' ';
  16.         }
  17.  
  18.         if (input[i] == ' ' && input[i + 1] == ' ') {
  19.             input.erase(input.begin() + i);
  20.         }
  21.     }
  22.     std::stringstream str(input);
  23.  
  24.     std::string token;
  25.     std::set<std::string> output;
  26.     while (str >> token)
  27.     {
  28.         output.insert(token);
  29.     }
  30.     return output;
  31. }
  32.  
  33. std::vector<char> readQueries() {
  34.     std::vector<char> targetLetters;
  35.     char c;
  36.     while (std::cin >> c && c != '.') {
  37.         targetLetters.push_back(c);
  38.     }
  39.     return targetLetters;
  40. }
  41.  
  42. std::vector<std::vector<std::string>> findWords(std::vector<char>& targetLetters, std::set<std::string>& words) {
  43.     std::vector<std::vector<std::string>> foundWords;
  44.  
  45.     bool isFound = false;
  46.     int targetLettersCount = targetLetters.size();
  47.  
  48.     for (int i = 0; i < targetLettersCount; ++i) {
  49.         std::vector<std::string> lineOfWords;
  50.         for (auto& word : words) {
  51.             for (auto& letter : word) {
  52.                 if (letter == targetLetters[i] || std::abs(targetLetters[i] - letter) == 32) {
  53.                     isFound = true;
  54.                     break;
  55.                 }
  56.             }
  57.             if (isFound) {
  58.                 lineOfWords.push_back(word);
  59.             }
  60.             isFound = false;
  61.         }
  62.         foundWords.push_back(lineOfWords);
  63.     }
  64.     return foundWords;
  65. }
  66.  
  67. void printSolution(std::vector<std::vector<std::string>>& foundWords) {
  68.     for (auto& line : foundWords) {
  69.         if (line.empty()) {
  70.             std::cout << "---";
  71.         }
  72.         else {
  73.             for (auto& word : line) {
  74.                 std::cout << word << ' ';
  75.             }
  76.         }
  77.         std::cout << std::endl;
  78.     }
  79. }
  80.  
  81. int main()
  82. {
  83.     auto words = getMessage();
  84.     auto targetLetters = readQueries();
  85.     auto outputWords = findWords(targetLetters, words);
  86.     printSolution(outputWords);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment