Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <string>
- #include <sstream>
- #include <cctype>
- #include <vector>
- #include <algorithm>
- std::set <std::string> getMessage() {
- std::string input;
- getline(std::cin, input);
- for (size_t i = 0; i < input.size(); ++i) {
- if (ispunct(input[i])) {
- input[i] = ' ';
- }
- if (input[i] == ' ' && input[i + 1] == ' ') {
- input.erase(input.begin() + i);
- }
- }
- std::stringstream str(input);
- std::string token;
- std::set<std::string> output;
- while (str >> token)
- {
- output.insert(token);
- }
- return output;
- }
- std::vector<char> readQueries() {
- std::vector<char> targetLetters;
- char c;
- while (std::cin >> c && c != '.') {
- targetLetters.push_back(c);
- }
- return targetLetters;
- }
- std::vector<std::vector<std::string>> findWords(std::vector<char>& targetLetters, std::set<std::string>& words) {
- std::vector<std::vector<std::string>> foundWords;
- bool isFound = false;
- int targetLettersCount = targetLetters.size();
- for (int i = 0; i < targetLettersCount; ++i) {
- std::vector<std::string> lineOfWords;
- for (auto& word : words) {
- for (auto& letter : word) {
- if (letter == targetLetters[i] || std::abs(targetLetters[i] - letter) == 32) {
- isFound = true;
- break;
- }
- }
- if (isFound) {
- lineOfWords.push_back(word);
- }
- isFound = false;
- }
- foundWords.push_back(lineOfWords);
- }
- return foundWords;
- }
- void printSolution(std::vector<std::vector<std::string>>& foundWords) {
- for (auto& line : foundWords) {
- if (line.empty()) {
- std::cout << "---";
- }
- else {
- for (auto& word : line) {
- std::cout << word << ' ';
- }
- }
- std::cout << std::endl;
- }
- }
- int main()
- {
- auto words = getMessage();
- auto targetLetters = readQueries();
- auto outputWords = findWords(targetLetters, words);
- printSolution(outputWords);
- }
Advertisement
Add Comment
Please, Sign In to add comment