Added C++ version with trie