Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <algorithm>
- #include <cctype>
- std::string readSentence() {
- std::string sentence;
- getline(std::cin, sentence);
- return sentence;
- }
- std::vector<char> readLetters() {
- std::vector<char> letters;
- char c; std::cin >> c;
- while (c != '.') {
- letters.push_back(c);
- std::cin >> c;
- }
- return letters;
- }
- void convertSentence(std::string& sentence, std::vector<std::string>& words) {
- for (size_t i = 0; i < sentence.size(); ++i) {
- if (ispunct(sentence[i])) {
- sentence[i] = ' ';
- }
- if (sentence[i] == ' ' && sentence[i + 1] == ' ') {
- sentence.erase(sentence.begin() + i);
- }
- }
- std::istringstream istr(sentence);
- std::string word;
- while (istr >> word) {
- words.push_back(word);
- }
- }
- void removeDuplicates(std::vector<std::string>& v) {
- auto end = v.end();
- for (auto it = v.begin(); it != end; ++it) {
- end = std::remove(it + 1, end, *it);
- }
- v.erase(end, v.end());
- }
- void findWords(std::vector<char>& targetLetters, std::vector<std::string>& words, std::vector<std::vector<std::string>>& outputWords) {
- bool found = false;
- for (int i = 0; i < targetLetters.size(); ++i) {
- std::vector<std::string> row;
- for (auto& word : words) {
- for (char c : word) {
- if (c == targetLetters[i] || std::abs(targetLetters[i] - c) == 32) {
- found = true;
- break;
- }
- }
- if (found) {
- row.push_back(word);
- }
- found = false;
- }
- outputWords.push_back(row);
- }
- }
- void sortAndPrintOutputWords(std::vector<std::vector<std::string>>& outputWords) {
- for (auto& row : outputWords) {
- if (row.empty()) {
- std::cout << "---";
- }
- else {
- sort(row.begin(), row.end());
- removeDuplicates(row);
- for (auto& word : row) {
- std::cout << word << ' ';
- }
- }
- std::cout << std::endl;
- }
- }
- int main() {
- std::string sentence = readSentence();
- std::vector<char> targetLetters = readLetters();
- std::vector<std::string> words;
- convertSentence(sentence, words);
- std::vector<std::vector<std::string>> outputWords;
- findWords(targetLetters, words, outputWords);
- sortAndPrintOutputWords(outputWords);
- }
Advertisement
Add Comment
Please, Sign In to add comment