diff options
1 files changed, 41 insertions, 0 deletions
| -rw-r--r-- | Seminar13/task02.cpp | 41 |
diff --git a/Seminar13/task02.cpp b/Seminar13/task02.cpp new file mode 100644 index 0000000..8d47b45 --- /dev/null +++ b/Seminar13/task02.cpp @@ -0,0 +1,41 @@ +#include<iostream> +#include<string> +#include<map> +#include<vector> + +bool canRead(const std::multimap<char, char>& graph, const std::string& word, size_t at) { + if(at == word.length() - 1) + return true; + + auto successors = graph.find(word[at]); // Може ли да си спестим това търсене? + + if(successors == graph.end()) + return false; + + while(successors != graph.end() && (*successors).first == word[at]) { + if((*successors).second == word[at + 1]) { + return canRead(graph, word, at + 1); + } + + ++successors; + } + + return false; +} + +int main() { + std::multimap<char, char> graph; + + graph.insert({'a', 'c'}); + graph.insert({'a', 'd'}); + graph.insert({'c', 'b'}); + graph.insert({'b', 'd'}); + graph.insert({'d', 'k'}); + graph.insert({'d', 'c'}); + graph.insert({'d', 'a'}); + + std::map<char, bool> visited; + std::string word = "adaz"; + + std::cout << canRead(graph, word, 0); +}
\ No newline at end of file |
