mstoyanov7

letters

Jul 1st, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <cctype>
  7.  
  8. std::string readSentence() {
  9.     std::string sentence;
  10.     getline(std::cin, sentence);
  11.     return sentence;
  12. }
  13.  
  14. std::vector<char> readLetters() {
  15.     std::vector<char> letters;
  16.     char c; std::cin >> c;
  17.     while (c != '.') {
  18.         letters.push_back(c);
  19.         std::cin >> c;
  20.     }
  21.     return letters;
  22. }
  23.  
  24. void convertSentence(std::string& sentence, std::vector<std::string>& words) {
  25.     for (size_t i = 0; i < sentence.size(); ++i) {
  26.         if (ispunct(sentence[i])) {
  27.             sentence[i] = ' ';
  28.         }
  29.  
  30.         if (sentence[i] == ' ' && sentence[i + 1] == ' ') {
  31.             sentence.erase(sentence.begin() + i);
  32.         }
  33.     }
  34.  
  35.     std::istringstream istr(sentence);
  36.     std::string word;
  37.     while (istr >> word) {
  38.         words.push_back(word);
  39.     }
  40. }
  41.  
  42. void removeDuplicates(std::vector<std::string>& v) {
  43.     auto end = v.end();
  44.     for (auto it = v.begin(); it != end; ++it) {
  45.         end = std::remove(it + 1, end, *it);
  46.     }
  47.     v.erase(end, v.end());
  48. }
  49.  
  50. void findWords(std::vector<char>& targetLetters, std::vector<std::string>& words, std::vector<std::vector<std::string>>& outputWords) {
  51.  
  52.     bool found = false;
  53.  
  54.     for (int i = 0; i < targetLetters.size(); ++i) {
  55.         std::vector<std::string> row;
  56.         for (auto& word : words) {
  57.             for (char c : word) {
  58.                 if (c == targetLetters[i] || std::abs(targetLetters[i] - c) == 32) {
  59.                     found = true;
  60.                     break;
  61.                 }
  62.             }
  63.             if (found) {
  64.                 row.push_back(word);
  65.             }
  66.             found = false;
  67.         }
  68.         outputWords.push_back(row);
  69.     }
  70. }
  71.  
  72. void sortAndPrintOutputWords(std::vector<std::vector<std::string>>& outputWords) {
  73.     for (auto& row : outputWords) {
  74.         if (row.empty()) {
  75.             std::cout << "---";
  76.         }
  77.         else {
  78.             sort(row.begin(), row.end());
  79.             removeDuplicates(row);
  80.             for (auto& word : row) {
  81.                 std::cout << word << ' ';
  82.             }
  83.         }
  84.         std::cout << std::endl;
  85.     }
  86. }
  87.  
  88.  
  89. int main() {
  90.     std::string sentence = readSentence();
  91.     std::vector<char> targetLetters = readLetters();
  92.  
  93.     std::vector<std::string> words;
  94.  
  95.     convertSentence(sentence, words);
  96.  
  97.     std::vector<std::vector<std::string>> outputWords;
  98.  
  99.     findWords(targetLetters, words, outputWords);
  100.  
  101.     sortAndPrintOutputWords(outputWords);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment