diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-14 06:35:14 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-14 06:47:40 +0100 |
commit | 26e4da9f87a7330d96bbeea0d47d875d3a96b1f8 (patch) | |
tree | cbce4f003f3f877968d70e8bf42dcb2bf755221e /04-algorithms_on_strings/01-suffix_trees/01-trie | |
parent | 45188449a04a1455ade01caf470e43a926d752aa (diff) | |
download | coursera-26e4da9f87a7330d96bbeea0d47d875d3a96b1f8.zip coursera-26e4da9f87a7330d96bbeea0d47d875d3a96b1f8.tar.gz |
Algorithms : complete 04-algorithms_on_strings 01-suffix_trees
Diffstat (limited to '04-algorithms_on_strings/01-suffix_trees/01-trie')
-rw-r--r-- | 04-algorithms_on_strings/01-suffix_trees/01-trie/trie.cpp | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/04-algorithms_on_strings/01-suffix_trees/01-trie/trie.cpp b/04-algorithms_on_strings/01-suffix_trees/01-trie/trie.cpp index 08cfe9b..d5f9483 100644 --- a/04-algorithms_on_strings/01-suffix_trees/01-trie/trie.cpp +++ b/04-algorithms_on_strings/01-suffix_trees/01-trie/trie.cpp @@ -12,7 +12,27 @@ typedef vector<edges> trie; trie build_trie(vector<string> & patterns) { trie t; - // write your code here + + size_t node; + size_t n = 1; + t.push_back(edges()); + + for (size_t i = 0; i < patterns.size(); i++) { + node = 0; + const string& pattern = patterns[i]; + for (size_t j = 0; j < pattern.size(); j++) { + char c = pattern[j]; + int next = t[node][c]; + if (next != 0) { + node = next; + } else { + t[node][c] = n; + node = n; + t.push_back(edges()); + n += 1; + } + } + } return t; } @@ -34,4 +54,4 @@ int main() { } return 0; -}
\ No newline at end of file +} |